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>
29 #include <drm/amdgpu_drm.h>
31 #include "amdgpu_trace.h"
33 int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type,
34 u32 ip_instance, u32 ring,
35 struct amdgpu_ring **out_ring)
37 /* Right now all IPs have only one instance - multiple rings. */
38 if (ip_instance != 0) {
39 DRM_ERROR("invalid ip instance: %d\n", ip_instance);
45 DRM_ERROR("unknown ip type: %d\n", ip_type);
47 case AMDGPU_HW_IP_GFX:
48 if (ring < adev->gfx.num_gfx_rings) {
49 *out_ring = &adev->gfx.gfx_ring[ring];
51 DRM_ERROR("only %d gfx rings are supported now\n",
52 adev->gfx.num_gfx_rings);
56 case AMDGPU_HW_IP_COMPUTE:
57 if (ring < adev->gfx.num_compute_rings) {
58 *out_ring = &adev->gfx.compute_ring[ring];
60 DRM_ERROR("only %d compute rings are supported now\n",
61 adev->gfx.num_compute_rings);
65 case AMDGPU_HW_IP_DMA:
66 if (ring < adev->sdma.num_instances) {
67 *out_ring = &adev->sdma.instance[ring].ring;
69 DRM_ERROR("only %d SDMA rings are supported\n",
70 adev->sdma.num_instances);
74 case AMDGPU_HW_IP_UVD:
75 *out_ring = &adev->uvd.ring;
77 case AMDGPU_HW_IP_VCE:
78 if (ring < adev->vce.num_rings){
79 *out_ring = &adev->vce.ring[ring];
81 DRM_ERROR("only %d VCE rings are supported\n", adev->vce.num_rings);
85 case AMDGPU_HW_IP_UVD_ENC:
86 if (ring < adev->uvd.num_enc_rings){
87 *out_ring = &adev->uvd.ring_enc[ring];
89 DRM_ERROR("only %d UVD ENC rings are supported\n",
90 adev->uvd.num_enc_rings);
96 if (!(*out_ring && (*out_ring)->adev)) {
97 DRM_ERROR("Ring %d is not initialized on IP %d\n",
105 static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
106 struct drm_amdgpu_cs_chunk_fence *data,
109 struct drm_gem_object *gobj;
112 gobj = drm_gem_object_lookup(p->filp, data->handle);
116 p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
117 p->uf_entry.priority = 0;
118 p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
119 p->uf_entry.tv.shared = true;
120 p->uf_entry.user_pages = NULL;
122 size = amdgpu_bo_size(p->uf_entry.robj);
123 if (size != PAGE_SIZE || (data->offset + 8) > size)
126 *offset = data->offset;
128 drm_gem_object_unreference_unlocked(gobj);
130 if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
131 amdgpu_bo_unref(&p->uf_entry.robj);
138 int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data)
140 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
141 struct amdgpu_vm *vm = &fpriv->vm;
142 union drm_amdgpu_cs *cs = data;
143 uint64_t *chunk_array_user;
144 uint64_t *chunk_array;
145 unsigned size, num_ibs = 0;
146 uint32_t uf_offset = 0;
150 if (cs->in.num_chunks == 0)
153 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
157 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
164 chunk_array_user = (uint64_t __user *)(uintptr_t)(cs->in.chunks);
165 if (copy_from_user(chunk_array, chunk_array_user,
166 sizeof(uint64_t)*cs->in.num_chunks)) {
171 p->nchunks = cs->in.num_chunks;
172 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
179 for (i = 0; i < p->nchunks; i++) {
180 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
181 struct drm_amdgpu_cs_chunk user_chunk;
182 uint32_t __user *cdata;
184 chunk_ptr = (void __user *)(uintptr_t)chunk_array[i];
185 if (copy_from_user(&user_chunk, chunk_ptr,
186 sizeof(struct drm_amdgpu_cs_chunk))) {
189 goto free_partial_kdata;
191 p->chunks[i].chunk_id = user_chunk.chunk_id;
192 p->chunks[i].length_dw = user_chunk.length_dw;
194 size = p->chunks[i].length_dw;
195 cdata = (void __user *)(uintptr_t)user_chunk.chunk_data;
197 p->chunks[i].kdata = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
198 if (p->chunks[i].kdata == NULL) {
201 goto free_partial_kdata;
203 size *= sizeof(uint32_t);
204 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
206 goto free_partial_kdata;
209 switch (p->chunks[i].chunk_id) {
210 case AMDGPU_CHUNK_ID_IB:
214 case AMDGPU_CHUNK_ID_FENCE:
215 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
216 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
218 goto free_partial_kdata;
221 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
224 goto free_partial_kdata;
228 case AMDGPU_CHUNK_ID_DEPENDENCIES:
233 goto free_partial_kdata;
237 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
241 if (p->uf_entry.robj)
242 p->job->uf_addr = uf_offset;
250 kvfree(p->chunks[i].kdata);
255 amdgpu_ctx_put(p->ctx);
262 /* Convert microseconds to bytes. */
263 static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
265 if (us <= 0 || !adev->mm_stats.log2_max_MBps)
268 /* Since accum_us is incremented by a million per second, just
269 * multiply it by the number of MB/s to get the number of bytes.
271 return us << adev->mm_stats.log2_max_MBps;
274 static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
276 if (!adev->mm_stats.log2_max_MBps)
279 return bytes >> adev->mm_stats.log2_max_MBps;
282 /* Returns how many bytes TTM can move right now. If no bytes can be moved,
283 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
284 * which means it can go over the threshold once. If that happens, the driver
285 * will be in debt and no other buffer migrations can be done until that debt
288 * This approach allows moving a buffer of any size (it's important to allow
291 * The currency is simply time in microseconds and it increases as the clock
292 * ticks. The accumulated microseconds (us) are converted to bytes and
295 static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev)
297 s64 time_us, increment_us;
299 u64 free_vram, total_vram, used_vram;
301 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
304 * It means that in order to get full max MBps, at least 5 IBs per
305 * second must be submitted and not more than 200ms apart from each
308 const s64 us_upper_bound = 200000;
310 if (!adev->mm_stats.log2_max_MBps)
313 total_vram = adev->mc.real_vram_size - adev->vram_pin_size;
314 used_vram = atomic64_read(&adev->vram_usage);
315 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
317 spin_lock(&adev->mm_stats.lock);
319 /* Increase the amount of accumulated us. */
320 time_us = ktime_to_us(ktime_get());
321 increment_us = time_us - adev->mm_stats.last_update_us;
322 adev->mm_stats.last_update_us = time_us;
323 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
326 /* This prevents the short period of low performance when the VRAM
327 * usage is low and the driver is in debt or doesn't have enough
328 * accumulated us to fill VRAM quickly.
330 * The situation can occur in these cases:
331 * - a lot of VRAM is freed by userspace
332 * - the presence of a big buffer causes a lot of evictions
333 * (solution: split buffers into smaller ones)
335 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
336 * accum_us to a positive number.
338 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
341 /* Be more aggresive on dGPUs. Try to fill a portion of free
344 if (!(adev->flags & AMD_IS_APU))
345 min_us = bytes_to_us(adev, free_vram / 4);
347 min_us = 0; /* Reset accum_us on APUs. */
349 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
352 /* This returns 0 if the driver is in debt to disallow (optional)
355 max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
357 spin_unlock(&adev->mm_stats.lock);
361 /* Report how many bytes have really been moved for the last command
362 * submission. This can result in a debt that can stop buffer migrations
365 void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes)
367 spin_lock(&adev->mm_stats.lock);
368 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
369 spin_unlock(&adev->mm_stats.lock);
372 static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
373 struct amdgpu_bo *bo)
375 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
376 u64 initial_bytes_moved;
383 /* Don't move this buffer if we have depleted our allowance
384 * to move it. Don't move anything if the threshold is zero.
386 if (p->bytes_moved < p->bytes_moved_threshold)
387 domain = bo->prefered_domains;
389 domain = bo->allowed_domains;
392 amdgpu_ttm_placement_from_domain(bo, domain);
393 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
394 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
395 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
398 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
399 domain = bo->allowed_domains;
406 /* Last resort, try to evict something from the current working set */
407 static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
408 struct amdgpu_bo *validated)
410 uint32_t domain = validated->allowed_domains;
416 for (;&p->evictable->tv.head != &p->validated;
417 p->evictable = list_prev_entry(p->evictable, tv.head)) {
419 struct amdgpu_bo_list_entry *candidate = p->evictable;
420 struct amdgpu_bo *bo = candidate->robj;
421 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
422 u64 initial_bytes_moved;
425 /* If we reached our current BO we can forget it */
426 if (candidate->robj == validated)
429 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
431 /* Check if this BO is in one of the domains we need space for */
432 if (!(other & domain))
435 /* Check if we can move this BO somewhere else */
436 other = bo->allowed_domains & ~domain;
440 /* Good we can try to move this BO somewhere else */
441 amdgpu_ttm_placement_from_domain(bo, other);
442 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved);
443 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
444 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) -
450 p->evictable = list_prev_entry(p->evictable, tv.head);
451 list_move(&candidate->tv.head, &p->validated);
459 static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
461 struct amdgpu_cs_parser *p = param;
465 r = amdgpu_cs_bo_validate(p, bo);
466 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
471 r = amdgpu_cs_bo_validate(p, bo->shadow);
476 static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
477 struct list_head *validated)
479 struct amdgpu_bo_list_entry *lobj;
482 list_for_each_entry(lobj, validated, tv.head) {
483 struct amdgpu_bo *bo = lobj->robj;
484 bool binding_userptr = false;
485 struct mm_struct *usermm;
487 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
488 if (usermm && usermm != current->mm)
491 /* Check if we have user pages and nobody bound the BO already */
492 if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) {
493 size_t size = sizeof(struct page *);
495 size *= bo->tbo.ttm->num_pages;
496 memcpy(bo->tbo.ttm->pages, lobj->user_pages, size);
497 binding_userptr = true;
500 if (p->evictable == lobj)
503 r = amdgpu_cs_validate(p, bo);
507 if (binding_userptr) {
508 kvfree(lobj->user_pages);
509 lobj->user_pages = NULL;
515 static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
516 union drm_amdgpu_cs *cs)
518 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
519 struct amdgpu_bo_list_entry *e;
520 struct list_head duplicates;
521 bool need_mmap_lock = false;
522 unsigned i, tries = 10;
525 INIT_LIST_HEAD(&p->validated);
527 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
529 need_mmap_lock = p->bo_list->first_userptr !=
530 p->bo_list->num_entries;
531 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
534 INIT_LIST_HEAD(&duplicates);
535 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
537 if (p->uf_entry.robj)
538 list_add(&p->uf_entry.tv.head, &p->validated);
541 down_read(¤t->mm->mmap_sem);
544 struct list_head need_pages;
547 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
549 if (unlikely(r != 0)) {
550 if (r != -ERESTARTSYS)
551 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
552 goto error_free_pages;
555 /* Without a BO list we don't have userptr BOs */
559 INIT_LIST_HEAD(&need_pages);
560 for (i = p->bo_list->first_userptr;
561 i < p->bo_list->num_entries; ++i) {
563 e = &p->bo_list->array[i];
565 if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm,
566 &e->user_invalidated) && e->user_pages) {
568 /* We acquired a page array, but somebody
569 * invalidated it. Free it an try again
571 release_pages(e->user_pages,
572 e->robj->tbo.ttm->num_pages,
574 kvfree(e->user_pages);
575 e->user_pages = NULL;
578 if (e->robj->tbo.ttm->state != tt_bound &&
580 list_del(&e->tv.head);
581 list_add(&e->tv.head, &need_pages);
583 amdgpu_bo_unreserve(e->robj);
587 if (list_empty(&need_pages))
590 /* Unreserve everything again. */
591 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
593 /* We tried too many times, just abort */
596 DRM_ERROR("deadlock in %s\n", __func__);
597 goto error_free_pages;
600 /* Fill the page arrays for all useptrs. */
601 list_for_each_entry(e, &need_pages, tv.head) {
602 struct ttm_tt *ttm = e->robj->tbo.ttm;
604 e->user_pages = kvmalloc_array(ttm->num_pages,
605 sizeof(struct page*),
606 GFP_KERNEL | __GFP_ZERO);
607 if (!e->user_pages) {
609 DRM_ERROR("calloc failure in %s\n", __func__);
610 goto error_free_pages;
613 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
615 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
616 kvfree(e->user_pages);
617 e->user_pages = NULL;
618 goto error_free_pages;
623 list_splice(&need_pages, &p->validated);
626 p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev);
628 p->evictable = list_last_entry(&p->validated,
629 struct amdgpu_bo_list_entry,
632 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
633 amdgpu_cs_validate, p);
635 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
639 r = amdgpu_cs_list_validate(p, &duplicates);
641 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
645 r = amdgpu_cs_list_validate(p, &p->validated);
647 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
651 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved);
653 fpriv->vm.last_eviction_counter =
654 atomic64_read(&p->adev->num_evictions);
657 struct amdgpu_bo *gds = p->bo_list->gds_obj;
658 struct amdgpu_bo *gws = p->bo_list->gws_obj;
659 struct amdgpu_bo *oa = p->bo_list->oa_obj;
660 struct amdgpu_vm *vm = &fpriv->vm;
663 for (i = 0; i < p->bo_list->num_entries; i++) {
664 struct amdgpu_bo *bo = p->bo_list->array[i].robj;
666 p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo);
670 p->job->gds_base = amdgpu_bo_gpu_offset(gds);
671 p->job->gds_size = amdgpu_bo_size(gds);
674 p->job->gws_base = amdgpu_bo_gpu_offset(gws);
675 p->job->gws_size = amdgpu_bo_size(gws);
678 p->job->oa_base = amdgpu_bo_gpu_offset(oa);
679 p->job->oa_size = amdgpu_bo_size(oa);
683 if (!r && p->uf_entry.robj) {
684 struct amdgpu_bo *uf = p->uf_entry.robj;
686 r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem);
687 p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
692 amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm);
693 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
699 up_read(¤t->mm->mmap_sem);
702 for (i = p->bo_list->first_userptr;
703 i < p->bo_list->num_entries; ++i) {
704 e = &p->bo_list->array[i];
709 release_pages(e->user_pages,
710 e->robj->tbo.ttm->num_pages,
712 kvfree(e->user_pages);
719 static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
721 struct amdgpu_bo_list_entry *e;
724 list_for_each_entry(e, &p->validated, tv.head) {
725 struct reservation_object *resv = e->robj->tbo.resv;
726 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp);
735 * cs_parser_fini() - clean parser states
736 * @parser: parser structure holding parsing context.
737 * @error: error number
739 * If error is set than unvalidate buffer, otherwise just free memory
740 * used by parsing context.
742 static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff)
744 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
748 amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm);
750 ttm_eu_fence_buffer_objects(&parser->ticket,
753 } else if (backoff) {
754 ttm_eu_backoff_reservation(&parser->ticket,
757 dma_fence_put(parser->fence);
760 amdgpu_ctx_put(parser->ctx);
762 amdgpu_bo_list_put(parser->bo_list);
764 for (i = 0; i < parser->nchunks; i++)
765 kvfree(parser->chunks[i].kdata);
766 kfree(parser->chunks);
768 amdgpu_job_free(parser->job);
769 amdgpu_bo_unref(&parser->uf_entry.robj);
772 static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p)
774 struct amdgpu_device *adev = p->adev;
775 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
776 struct amdgpu_vm *vm = &fpriv->vm;
777 struct amdgpu_bo_va *bo_va;
778 struct amdgpu_bo *bo;
781 r = amdgpu_vm_update_directories(adev, vm);
785 r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_dir_update);
789 r = amdgpu_vm_clear_freed(adev, vm, NULL);
793 r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
797 r = amdgpu_sync_fence(adev, &p->job->sync,
798 fpriv->prt_va->last_pt_update);
802 if (amdgpu_sriov_vf(adev)) {
804 bo_va = vm->csa_bo_va;
806 r = amdgpu_vm_bo_update(adev, bo_va, false);
810 f = bo_va->last_pt_update;
811 r = amdgpu_sync_fence(adev, &p->job->sync, f);
817 for (i = 0; i < p->bo_list->num_entries; i++) {
820 /* ignore duplicates */
821 bo = p->bo_list->array[i].robj;
825 bo_va = p->bo_list->array[i].bo_va;
829 r = amdgpu_vm_bo_update(adev, bo_va, false);
833 f = bo_va->last_pt_update;
834 r = amdgpu_sync_fence(adev, &p->job->sync, f);
841 r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync);
843 if (amdgpu_vm_debug && p->bo_list) {
844 /* Invalidate all BOs to test for userspace bugs */
845 for (i = 0; i < p->bo_list->num_entries; i++) {
846 /* ignore duplicates */
847 bo = p->bo_list->array[i].robj;
851 amdgpu_vm_bo_invalidate(adev, bo);
858 static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev,
859 struct amdgpu_cs_parser *p)
861 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
862 struct amdgpu_vm *vm = &fpriv->vm;
863 struct amdgpu_ring *ring = p->job->ring;
866 /* Only for UVD/VCE VM emulation */
867 if (ring->funcs->parse_cs) {
868 for (i = 0; i < p->job->num_ibs; i++) {
869 r = amdgpu_ring_parse_cs(ring, p, i);
876 p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->root.bo);
878 r = amdgpu_bo_vm_update_pte(p);
883 return amdgpu_cs_sync_rings(p);
886 static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
887 struct amdgpu_cs_parser *parser)
889 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
890 struct amdgpu_vm *vm = &fpriv->vm;
892 int r, ce_preempt = 0, de_preempt = 0;
894 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
895 struct amdgpu_cs_chunk *chunk;
896 struct amdgpu_ib *ib;
897 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
898 struct amdgpu_ring *ring;
900 chunk = &parser->chunks[i];
901 ib = &parser->job->ibs[j];
902 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
904 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
907 if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) {
908 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
909 if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
915 /* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */
916 if (ce_preempt > 1 || de_preempt > 1)
920 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type,
921 chunk_ib->ip_instance, chunk_ib->ring,
926 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE) {
927 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
928 if (!parser->ctx->preamble_presented) {
929 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
930 parser->ctx->preamble_presented = true;
934 if (parser->job->ring && parser->job->ring != ring)
937 parser->job->ring = ring;
939 if (ring->funcs->parse_cs) {
940 struct amdgpu_bo_va_mapping *m;
941 struct amdgpu_bo *aobj = NULL;
945 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start,
948 DRM_ERROR("IB va_start is invalid\n");
952 if ((chunk_ib->va_start + chunk_ib->ib_bytes) >
953 (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
954 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
958 /* the IB should be reserved at this point */
959 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
964 offset = m->start * AMDGPU_GPU_PAGE_SIZE;
965 kptr += chunk_ib->va_start - offset;
967 r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib);
969 DRM_ERROR("Failed to get ib !\n");
973 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
974 amdgpu_bo_kunmap(aobj);
976 r = amdgpu_ib_get(adev, vm, 0, ib);
978 DRM_ERROR("Failed to get ib !\n");
984 ib->gpu_addr = chunk_ib->va_start;
985 ib->length_dw = chunk_ib->ib_bytes / 4;
986 ib->flags = chunk_ib->flags;
990 /* UVD & VCE fw doesn't support user fences */
991 if (parser->job->uf_addr && (
992 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
993 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE))
999 static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
1000 struct amdgpu_cs_parser *p)
1002 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1005 for (i = 0; i < p->nchunks; ++i) {
1006 struct drm_amdgpu_cs_chunk_dep *deps;
1007 struct amdgpu_cs_chunk *chunk;
1010 chunk = &p->chunks[i];
1012 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES)
1015 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
1016 num_deps = chunk->length_dw * 4 /
1017 sizeof(struct drm_amdgpu_cs_chunk_dep);
1019 for (j = 0; j < num_deps; ++j) {
1020 struct amdgpu_ring *ring;
1021 struct amdgpu_ctx *ctx;
1022 struct dma_fence *fence;
1024 r = amdgpu_cs_get_ring(adev, deps[j].ip_type,
1025 deps[j].ip_instance,
1026 deps[j].ring, &ring);
1030 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id);
1034 fence = amdgpu_ctx_get_fence(ctx, ring,
1036 if (IS_ERR(fence)) {
1038 amdgpu_ctx_put(ctx);
1042 r = amdgpu_sync_fence(adev, &p->job->sync,
1044 dma_fence_put(fence);
1045 amdgpu_ctx_put(ctx);
1055 static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1056 union drm_amdgpu_cs *cs)
1058 struct amdgpu_ring *ring = p->job->ring;
1059 struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity;
1060 struct amdgpu_job *job;
1066 r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp);
1068 amdgpu_job_free(job);
1072 job->owner = p->filp;
1073 job->fence_ctx = entity->fence_context;
1074 p->fence = dma_fence_get(&job->base.s_fence->finished);
1075 cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence);
1076 job->uf_sequence = cs->out.handle;
1077 amdgpu_job_free_resources(job);
1078 amdgpu_cs_parser_fini(p, 0, true);
1080 trace_amdgpu_cs_ioctl(job);
1081 amd_sched_entity_push_job(&job->base);
1086 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1088 struct amdgpu_device *adev = dev->dev_private;
1089 union drm_amdgpu_cs *cs = data;
1090 struct amdgpu_cs_parser parser = {};
1091 bool reserved_buffers = false;
1094 if (!adev->accel_working)
1100 r = amdgpu_cs_parser_init(&parser, data);
1102 DRM_ERROR("Failed to initialize parser !\n");
1106 r = amdgpu_cs_parser_bos(&parser, data);
1109 DRM_ERROR("Not enough memory for command submission!\n");
1110 else if (r != -ERESTARTSYS)
1111 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1115 reserved_buffers = true;
1116 r = amdgpu_cs_ib_fill(adev, &parser);
1120 r = amdgpu_cs_dependencies(adev, &parser);
1122 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1126 for (i = 0; i < parser.job->num_ibs; i++)
1127 trace_amdgpu_cs(&parser, i);
1129 r = amdgpu_cs_ib_vm_chunk(adev, &parser);
1133 r = amdgpu_cs_submit(&parser, cs);
1139 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
1144 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1147 * @data: data from userspace
1148 * @filp: file private
1150 * Wait for the command submission identified by handle to finish.
1152 int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1153 struct drm_file *filp)
1155 union drm_amdgpu_wait_cs *wait = data;
1156 struct amdgpu_device *adev = dev->dev_private;
1157 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
1158 struct amdgpu_ring *ring = NULL;
1159 struct amdgpu_ctx *ctx;
1160 struct dma_fence *fence;
1163 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance,
1164 wait->in.ring, &ring);
1168 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1172 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle);
1176 r = dma_fence_wait_timeout(fence, true, timeout);
1177 dma_fence_put(fence);
1181 amdgpu_ctx_put(ctx);
1185 memset(wait, 0, sizeof(*wait));
1186 wait->out.status = (r == 0);
1192 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1194 * @adev: amdgpu device
1195 * @filp: file private
1196 * @user: drm_amdgpu_fence copied from user space
1198 static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1199 struct drm_file *filp,
1200 struct drm_amdgpu_fence *user)
1202 struct amdgpu_ring *ring;
1203 struct amdgpu_ctx *ctx;
1204 struct dma_fence *fence;
1207 r = amdgpu_cs_get_ring(adev, user->ip_type, user->ip_instance,
1212 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1214 return ERR_PTR(-EINVAL);
1216 fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no);
1217 amdgpu_ctx_put(ctx);
1223 * amdgpu_cs_wait_all_fence - wait on all fences to signal
1225 * @adev: amdgpu device
1226 * @filp: file private
1227 * @wait: wait parameters
1228 * @fences: array of drm_amdgpu_fence
1230 static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1231 struct drm_file *filp,
1232 union drm_amdgpu_wait_fences *wait,
1233 struct drm_amdgpu_fence *fences)
1235 uint32_t fence_count = wait->in.fence_count;
1239 for (i = 0; i < fence_count; i++) {
1240 struct dma_fence *fence;
1241 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1243 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1245 return PTR_ERR(fence);
1249 r = dma_fence_wait_timeout(fence, true, timeout);
1250 dma_fence_put(fence);
1258 memset(wait, 0, sizeof(*wait));
1259 wait->out.status = (r > 0);
1265 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1267 * @adev: amdgpu device
1268 * @filp: file private
1269 * @wait: wait parameters
1270 * @fences: array of drm_amdgpu_fence
1272 static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1273 struct drm_file *filp,
1274 union drm_amdgpu_wait_fences *wait,
1275 struct drm_amdgpu_fence *fences)
1277 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1278 uint32_t fence_count = wait->in.fence_count;
1279 uint32_t first = ~0;
1280 struct dma_fence **array;
1284 /* Prepare the fence array */
1285 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1290 for (i = 0; i < fence_count; i++) {
1291 struct dma_fence *fence;
1293 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1294 if (IS_ERR(fence)) {
1296 goto err_free_fence_array;
1299 } else { /* NULL, the fence has been already signaled */
1305 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1308 goto err_free_fence_array;
1311 memset(wait, 0, sizeof(*wait));
1312 wait->out.status = (r > 0);
1313 wait->out.first_signaled = first;
1314 /* set return value 0 to indicate success */
1317 err_free_fence_array:
1318 for (i = 0; i < fence_count; i++)
1319 dma_fence_put(array[i]);
1326 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1329 * @data: data from userspace
1330 * @filp: file private
1332 int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1333 struct drm_file *filp)
1335 struct amdgpu_device *adev = dev->dev_private;
1336 union drm_amdgpu_wait_fences *wait = data;
1337 uint32_t fence_count = wait->in.fence_count;
1338 struct drm_amdgpu_fence *fences_user;
1339 struct drm_amdgpu_fence *fences;
1342 /* Get the fences from userspace */
1343 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1348 fences_user = (void __user *)(uintptr_t)(wait->in.fences);
1349 if (copy_from_user(fences, fences_user,
1350 sizeof(struct drm_amdgpu_fence) * fence_count)) {
1352 goto err_free_fences;
1355 if (wait->in.wait_all)
1356 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1358 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1367 * amdgpu_cs_find_bo_va - find bo_va for VM address
1369 * @parser: command submission parser context
1371 * @bo: resulting BO of the mapping found
1373 * Search the buffer objects in the command submission context for a certain
1374 * virtual memory address. Returns allocation structure when found, NULL
1377 struct amdgpu_bo_va_mapping *
1378 amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1379 uint64_t addr, struct amdgpu_bo **bo)
1381 struct amdgpu_bo_va_mapping *mapping;
1384 if (!parser->bo_list)
1387 addr /= AMDGPU_GPU_PAGE_SIZE;
1389 for (i = 0; i < parser->bo_list->num_entries; i++) {
1390 struct amdgpu_bo_list_entry *lobj;
1392 lobj = &parser->bo_list->array[i];
1396 list_for_each_entry(mapping, &lobj->bo_va->valids, list) {
1397 if (mapping->start > addr ||
1398 addr > mapping->last)
1401 *bo = lobj->bo_va->bo;
1405 list_for_each_entry(mapping, &lobj->bo_va->invalids, list) {
1406 if (mapping->start > addr ||
1407 addr > mapping->last)
1410 *bo = lobj->bo_va->bo;
1419 * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
1421 * @parser: command submission parser context
1423 * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
1425 int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser)
1430 if (!parser->bo_list)
1433 for (i = 0; i < parser->bo_list->num_entries; i++) {
1434 struct amdgpu_bo *bo = parser->bo_list->array[i].robj;
1436 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
1440 if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
1443 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1444 amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains);
1445 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);