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>
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
);
87 if (!(*out_ring
&& (*out_ring
)->adev
)) {
88 DRM_ERROR("Ring %d is not initialized on IP %d\n",
96 static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser
*p
,
97 struct drm_amdgpu_cs_chunk_fence
*data
,
100 struct drm_gem_object
*gobj
;
103 gobj
= drm_gem_object_lookup(p
->filp
, data
->handle
);
107 p
->uf_entry
.robj
= amdgpu_bo_ref(gem_to_amdgpu_bo(gobj
));
108 p
->uf_entry
.priority
= 0;
109 p
->uf_entry
.tv
.bo
= &p
->uf_entry
.robj
->tbo
;
110 p
->uf_entry
.tv
.shared
= true;
111 p
->uf_entry
.user_pages
= NULL
;
113 size
= amdgpu_bo_size(p
->uf_entry
.robj
);
114 if (size
!= PAGE_SIZE
|| (data
->offset
+ 8) > size
)
117 *offset
= data
->offset
;
119 drm_gem_object_unreference_unlocked(gobj
);
121 if (amdgpu_ttm_tt_get_usermm(p
->uf_entry
.robj
->tbo
.ttm
)) {
122 amdgpu_bo_unref(&p
->uf_entry
.robj
);
129 int amdgpu_cs_parser_init(struct amdgpu_cs_parser
*p
, void *data
)
131 struct amdgpu_fpriv
*fpriv
= p
->filp
->driver_priv
;
132 struct amdgpu_vm
*vm
= &fpriv
->vm
;
133 union drm_amdgpu_cs
*cs
= data
;
134 uint64_t *chunk_array_user
;
135 uint64_t *chunk_array
;
136 unsigned size
, num_ibs
= 0;
137 uint32_t uf_offset
= 0;
141 if (cs
->in
.num_chunks
== 0)
144 chunk_array
= kmalloc_array(cs
->in
.num_chunks
, sizeof(uint64_t), GFP_KERNEL
);
148 p
->ctx
= amdgpu_ctx_get(fpriv
, cs
->in
.ctx_id
);
155 chunk_array_user
= (uint64_t __user
*)(unsigned long)(cs
->in
.chunks
);
156 if (copy_from_user(chunk_array
, chunk_array_user
,
157 sizeof(uint64_t)*cs
->in
.num_chunks
)) {
162 p
->nchunks
= cs
->in
.num_chunks
;
163 p
->chunks
= kmalloc_array(p
->nchunks
, sizeof(struct amdgpu_cs_chunk
),
170 for (i
= 0; i
< p
->nchunks
; i
++) {
171 struct drm_amdgpu_cs_chunk __user
**chunk_ptr
= NULL
;
172 struct drm_amdgpu_cs_chunk user_chunk
;
173 uint32_t __user
*cdata
;
175 chunk_ptr
= (void __user
*)(unsigned long)chunk_array
[i
];
176 if (copy_from_user(&user_chunk
, chunk_ptr
,
177 sizeof(struct drm_amdgpu_cs_chunk
))) {
180 goto free_partial_kdata
;
182 p
->chunks
[i
].chunk_id
= user_chunk
.chunk_id
;
183 p
->chunks
[i
].length_dw
= user_chunk
.length_dw
;
185 size
= p
->chunks
[i
].length_dw
;
186 cdata
= (void __user
*)(unsigned long)user_chunk
.chunk_data
;
188 p
->chunks
[i
].kdata
= drm_malloc_ab(size
, sizeof(uint32_t));
189 if (p
->chunks
[i
].kdata
== NULL
) {
192 goto free_partial_kdata
;
194 size
*= sizeof(uint32_t);
195 if (copy_from_user(p
->chunks
[i
].kdata
, cdata
, size
)) {
197 goto free_partial_kdata
;
200 switch (p
->chunks
[i
].chunk_id
) {
201 case AMDGPU_CHUNK_ID_IB
:
205 case AMDGPU_CHUNK_ID_FENCE
:
206 size
= sizeof(struct drm_amdgpu_cs_chunk_fence
);
207 if (p
->chunks
[i
].length_dw
* sizeof(uint32_t) < size
) {
209 goto free_partial_kdata
;
212 ret
= amdgpu_cs_user_fence_chunk(p
, p
->chunks
[i
].kdata
,
215 goto free_partial_kdata
;
219 case AMDGPU_CHUNK_ID_DEPENDENCIES
:
224 goto free_partial_kdata
;
228 ret
= amdgpu_job_alloc(p
->adev
, num_ibs
, &p
->job
, vm
);
232 if (p
->uf_entry
.robj
)
233 p
->job
->uf_addr
= uf_offset
;
241 drm_free_large(p
->chunks
[i
].kdata
);
244 amdgpu_ctx_put(p
->ctx
);
251 /* Convert microseconds to bytes. */
252 static u64
us_to_bytes(struct amdgpu_device
*adev
, s64 us
)
254 if (us
<= 0 || !adev
->mm_stats
.log2_max_MBps
)
257 /* Since accum_us is incremented by a million per second, just
258 * multiply it by the number of MB/s to get the number of bytes.
260 return us
<< adev
->mm_stats
.log2_max_MBps
;
263 static s64
bytes_to_us(struct amdgpu_device
*adev
, u64 bytes
)
265 if (!adev
->mm_stats
.log2_max_MBps
)
268 return bytes
>> adev
->mm_stats
.log2_max_MBps
;
271 /* Returns how many bytes TTM can move right now. If no bytes can be moved,
272 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
273 * which means it can go over the threshold once. If that happens, the driver
274 * will be in debt and no other buffer migrations can be done until that debt
277 * This approach allows moving a buffer of any size (it's important to allow
280 * The currency is simply time in microseconds and it increases as the clock
281 * ticks. The accumulated microseconds (us) are converted to bytes and
284 static u64
amdgpu_cs_get_threshold_for_moves(struct amdgpu_device
*adev
)
286 s64 time_us
, increment_us
;
288 u64 free_vram
, total_vram
, used_vram
;
290 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
293 * It means that in order to get full max MBps, at least 5 IBs per
294 * second must be submitted and not more than 200ms apart from each
297 const s64 us_upper_bound
= 200000;
299 if (!adev
->mm_stats
.log2_max_MBps
)
302 total_vram
= adev
->mc
.real_vram_size
- adev
->vram_pin_size
;
303 used_vram
= atomic64_read(&adev
->vram_usage
);
304 free_vram
= used_vram
>= total_vram
? 0 : total_vram
- used_vram
;
306 spin_lock(&adev
->mm_stats
.lock
);
308 /* Increase the amount of accumulated us. */
309 time_us
= ktime_to_us(ktime_get());
310 increment_us
= time_us
- adev
->mm_stats
.last_update_us
;
311 adev
->mm_stats
.last_update_us
= time_us
;
312 adev
->mm_stats
.accum_us
= min(adev
->mm_stats
.accum_us
+ increment_us
,
315 /* This prevents the short period of low performance when the VRAM
316 * usage is low and the driver is in debt or doesn't have enough
317 * accumulated us to fill VRAM quickly.
319 * The situation can occur in these cases:
320 * - a lot of VRAM is freed by userspace
321 * - the presence of a big buffer causes a lot of evictions
322 * (solution: split buffers into smaller ones)
324 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
325 * accum_us to a positive number.
327 if (free_vram
>= 128 * 1024 * 1024 || free_vram
>= total_vram
/ 8) {
330 /* Be more aggresive on dGPUs. Try to fill a portion of free
333 if (!(adev
->flags
& AMD_IS_APU
))
334 min_us
= bytes_to_us(adev
, free_vram
/ 4);
336 min_us
= 0; /* Reset accum_us on APUs. */
338 adev
->mm_stats
.accum_us
= max(min_us
, adev
->mm_stats
.accum_us
);
341 /* This returns 0 if the driver is in debt to disallow (optional)
344 max_bytes
= us_to_bytes(adev
, adev
->mm_stats
.accum_us
);
346 spin_unlock(&adev
->mm_stats
.lock
);
350 /* Report how many bytes have really been moved for the last command
351 * submission. This can result in a debt that can stop buffer migrations
354 void amdgpu_cs_report_moved_bytes(struct amdgpu_device
*adev
, u64 num_bytes
)
356 spin_lock(&adev
->mm_stats
.lock
);
357 adev
->mm_stats
.accum_us
-= bytes_to_us(adev
, num_bytes
);
358 spin_unlock(&adev
->mm_stats
.lock
);
361 static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser
*p
,
362 struct amdgpu_bo
*bo
)
364 struct amdgpu_device
*adev
= amdgpu_ttm_adev(bo
->tbo
.bdev
);
365 u64 initial_bytes_moved
;
372 /* Don't move this buffer if we have depleted our allowance
373 * to move it. Don't move anything if the threshold is zero.
375 if (p
->bytes_moved
< p
->bytes_moved_threshold
)
376 domain
= bo
->prefered_domains
;
378 domain
= bo
->allowed_domains
;
381 amdgpu_ttm_placement_from_domain(bo
, domain
);
382 initial_bytes_moved
= atomic64_read(&adev
->num_bytes_moved
);
383 r
= ttm_bo_validate(&bo
->tbo
, &bo
->placement
, true, false);
384 p
->bytes_moved
+= atomic64_read(&adev
->num_bytes_moved
) -
387 if (unlikely(r
== -ENOMEM
) && domain
!= bo
->allowed_domains
) {
388 domain
= bo
->allowed_domains
;
395 /* Last resort, try to evict something from the current working set */
396 static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser
*p
,
397 struct amdgpu_bo
*validated
)
399 uint32_t domain
= validated
->allowed_domains
;
405 for (;&p
->evictable
->tv
.head
!= &p
->validated
;
406 p
->evictable
= list_prev_entry(p
->evictable
, tv
.head
)) {
408 struct amdgpu_bo_list_entry
*candidate
= p
->evictable
;
409 struct amdgpu_bo
*bo
= candidate
->robj
;
410 struct amdgpu_device
*adev
= amdgpu_ttm_adev(bo
->tbo
.bdev
);
411 u64 initial_bytes_moved
;
414 /* If we reached our current BO we can forget it */
415 if (candidate
->robj
== validated
)
418 other
= amdgpu_mem_type_to_domain(bo
->tbo
.mem
.mem_type
);
420 /* Check if this BO is in one of the domains we need space for */
421 if (!(other
& domain
))
424 /* Check if we can move this BO somewhere else */
425 other
= bo
->allowed_domains
& ~domain
;
429 /* Good we can try to move this BO somewhere else */
430 amdgpu_ttm_placement_from_domain(bo
, other
);
431 initial_bytes_moved
= atomic64_read(&adev
->num_bytes_moved
);
432 r
= ttm_bo_validate(&bo
->tbo
, &bo
->placement
, true, false);
433 p
->bytes_moved
+= atomic64_read(&adev
->num_bytes_moved
) -
439 p
->evictable
= list_prev_entry(p
->evictable
, tv
.head
);
440 list_move(&candidate
->tv
.head
, &p
->validated
);
448 static int amdgpu_cs_validate(void *param
, struct amdgpu_bo
*bo
)
450 struct amdgpu_cs_parser
*p
= param
;
454 r
= amdgpu_cs_bo_validate(p
, bo
);
455 } while (r
== -ENOMEM
&& amdgpu_cs_try_evict(p
, bo
));
460 r
= amdgpu_cs_bo_validate(p
, bo
->shadow
);
465 static int amdgpu_cs_list_validate(struct amdgpu_cs_parser
*p
,
466 struct list_head
*validated
)
468 struct amdgpu_bo_list_entry
*lobj
;
471 list_for_each_entry(lobj
, validated
, tv
.head
) {
472 struct amdgpu_bo
*bo
= lobj
->robj
;
473 bool binding_userptr
= false;
474 struct mm_struct
*usermm
;
476 usermm
= amdgpu_ttm_tt_get_usermm(bo
->tbo
.ttm
);
477 if (usermm
&& usermm
!= current
->mm
)
480 /* Check if we have user pages and nobody bound the BO already */
481 if (lobj
->user_pages
&& bo
->tbo
.ttm
->state
!= tt_bound
) {
482 size_t size
= sizeof(struct page
*);
484 size
*= bo
->tbo
.ttm
->num_pages
;
485 memcpy(bo
->tbo
.ttm
->pages
, lobj
->user_pages
, size
);
486 binding_userptr
= true;
489 if (p
->evictable
== lobj
)
492 r
= amdgpu_cs_validate(p
, bo
);
496 if (binding_userptr
) {
497 drm_free_large(lobj
->user_pages
);
498 lobj
->user_pages
= NULL
;
504 static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser
*p
,
505 union drm_amdgpu_cs
*cs
)
507 struct amdgpu_fpriv
*fpriv
= p
->filp
->driver_priv
;
508 struct amdgpu_bo_list_entry
*e
;
509 struct list_head duplicates
;
510 bool need_mmap_lock
= false;
511 unsigned i
, tries
= 10;
514 INIT_LIST_HEAD(&p
->validated
);
516 p
->bo_list
= amdgpu_bo_list_get(fpriv
, cs
->in
.bo_list_handle
);
518 need_mmap_lock
= p
->bo_list
->first_userptr
!=
519 p
->bo_list
->num_entries
;
520 amdgpu_bo_list_get_list(p
->bo_list
, &p
->validated
);
523 INIT_LIST_HEAD(&duplicates
);
524 amdgpu_vm_get_pd_bo(&fpriv
->vm
, &p
->validated
, &p
->vm_pd
);
526 if (p
->uf_entry
.robj
)
527 list_add(&p
->uf_entry
.tv
.head
, &p
->validated
);
530 down_read(¤t
->mm
->mmap_sem
);
533 struct list_head need_pages
;
536 r
= ttm_eu_reserve_buffers(&p
->ticket
, &p
->validated
, true,
538 if (unlikely(r
!= 0)) {
539 if (r
!= -ERESTARTSYS
)
540 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
541 goto error_free_pages
;
544 /* Without a BO list we don't have userptr BOs */
548 INIT_LIST_HEAD(&need_pages
);
549 for (i
= p
->bo_list
->first_userptr
;
550 i
< p
->bo_list
->num_entries
; ++i
) {
552 e
= &p
->bo_list
->array
[i
];
554 if (amdgpu_ttm_tt_userptr_invalidated(e
->robj
->tbo
.ttm
,
555 &e
->user_invalidated
) && e
->user_pages
) {
557 /* We acquired a page array, but somebody
558 * invalidated it. Free it an try again
560 release_pages(e
->user_pages
,
561 e
->robj
->tbo
.ttm
->num_pages
,
563 drm_free_large(e
->user_pages
);
564 e
->user_pages
= NULL
;
567 if (e
->robj
->tbo
.ttm
->state
!= tt_bound
&&
569 list_del(&e
->tv
.head
);
570 list_add(&e
->tv
.head
, &need_pages
);
572 amdgpu_bo_unreserve(e
->robj
);
576 if (list_empty(&need_pages
))
579 /* Unreserve everything again. */
580 ttm_eu_backoff_reservation(&p
->ticket
, &p
->validated
);
582 /* We tried too many times, just abort */
585 DRM_ERROR("deadlock in %s\n", __func__
);
586 goto error_free_pages
;
589 /* Fill the page arrays for all useptrs. */
590 list_for_each_entry(e
, &need_pages
, tv
.head
) {
591 struct ttm_tt
*ttm
= e
->robj
->tbo
.ttm
;
593 e
->user_pages
= drm_calloc_large(ttm
->num_pages
,
594 sizeof(struct page
*));
595 if (!e
->user_pages
) {
597 DRM_ERROR("calloc failure in %s\n", __func__
);
598 goto error_free_pages
;
601 r
= amdgpu_ttm_tt_get_user_pages(ttm
, e
->user_pages
);
603 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
604 drm_free_large(e
->user_pages
);
605 e
->user_pages
= NULL
;
606 goto error_free_pages
;
611 list_splice(&need_pages
, &p
->validated
);
614 p
->bytes_moved_threshold
= amdgpu_cs_get_threshold_for_moves(p
->adev
);
616 p
->evictable
= list_last_entry(&p
->validated
,
617 struct amdgpu_bo_list_entry
,
620 r
= amdgpu_vm_validate_pt_bos(p
->adev
, &fpriv
->vm
,
621 amdgpu_cs_validate
, p
);
623 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
627 r
= amdgpu_cs_list_validate(p
, &duplicates
);
629 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
633 r
= amdgpu_cs_list_validate(p
, &p
->validated
);
635 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
639 amdgpu_cs_report_moved_bytes(p
->adev
, p
->bytes_moved
);
641 fpriv
->vm
.last_eviction_counter
=
642 atomic64_read(&p
->adev
->num_evictions
);
645 struct amdgpu_bo
*gds
= p
->bo_list
->gds_obj
;
646 struct amdgpu_bo
*gws
= p
->bo_list
->gws_obj
;
647 struct amdgpu_bo
*oa
= p
->bo_list
->oa_obj
;
648 struct amdgpu_vm
*vm
= &fpriv
->vm
;
651 for (i
= 0; i
< p
->bo_list
->num_entries
; i
++) {
652 struct amdgpu_bo
*bo
= p
->bo_list
->array
[i
].robj
;
654 p
->bo_list
->array
[i
].bo_va
= amdgpu_vm_bo_find(vm
, bo
);
658 p
->job
->gds_base
= amdgpu_bo_gpu_offset(gds
);
659 p
->job
->gds_size
= amdgpu_bo_size(gds
);
662 p
->job
->gws_base
= amdgpu_bo_gpu_offset(gws
);
663 p
->job
->gws_size
= amdgpu_bo_size(gws
);
666 p
->job
->oa_base
= amdgpu_bo_gpu_offset(oa
);
667 p
->job
->oa_size
= amdgpu_bo_size(oa
);
671 if (!r
&& p
->uf_entry
.robj
) {
672 struct amdgpu_bo
*uf
= p
->uf_entry
.robj
;
674 r
= amdgpu_ttm_bind(&uf
->tbo
, &uf
->tbo
.mem
);
675 p
->job
->uf_addr
+= amdgpu_bo_gpu_offset(uf
);
680 amdgpu_vm_move_pt_bos_in_lru(p
->adev
, &fpriv
->vm
);
681 ttm_eu_backoff_reservation(&p
->ticket
, &p
->validated
);
687 up_read(¤t
->mm
->mmap_sem
);
690 for (i
= p
->bo_list
->first_userptr
;
691 i
< p
->bo_list
->num_entries
; ++i
) {
692 e
= &p
->bo_list
->array
[i
];
697 release_pages(e
->user_pages
,
698 e
->robj
->tbo
.ttm
->num_pages
,
700 drm_free_large(e
->user_pages
);
707 static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser
*p
)
709 struct amdgpu_bo_list_entry
*e
;
712 list_for_each_entry(e
, &p
->validated
, tv
.head
) {
713 struct reservation_object
*resv
= e
->robj
->tbo
.resv
;
714 r
= amdgpu_sync_resv(p
->adev
, &p
->job
->sync
, resv
, p
->filp
);
723 * cs_parser_fini() - clean parser states
724 * @parser: parser structure holding parsing context.
725 * @error: error number
727 * If error is set than unvalidate buffer, otherwise just free memory
728 * used by parsing context.
730 static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser
*parser
, int error
, bool backoff
)
732 struct amdgpu_fpriv
*fpriv
= parser
->filp
->driver_priv
;
736 amdgpu_vm_move_pt_bos_in_lru(parser
->adev
, &fpriv
->vm
);
738 ttm_eu_fence_buffer_objects(&parser
->ticket
,
741 } else if (backoff
) {
742 ttm_eu_backoff_reservation(&parser
->ticket
,
745 dma_fence_put(parser
->fence
);
748 amdgpu_ctx_put(parser
->ctx
);
750 amdgpu_bo_list_put(parser
->bo_list
);
752 for (i
= 0; i
< parser
->nchunks
; i
++)
753 drm_free_large(parser
->chunks
[i
].kdata
);
754 kfree(parser
->chunks
);
756 amdgpu_job_free(parser
->job
);
757 amdgpu_bo_unref(&parser
->uf_entry
.robj
);
760 static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser
*p
,
761 struct amdgpu_vm
*vm
)
763 struct amdgpu_device
*adev
= p
->adev
;
764 struct amdgpu_bo_va
*bo_va
;
765 struct amdgpu_bo
*bo
;
768 r
= amdgpu_vm_update_page_directory(adev
, vm
);
772 r
= amdgpu_sync_fence(adev
, &p
->job
->sync
, vm
->page_directory_fence
);
776 r
= amdgpu_vm_clear_freed(adev
, vm
);
780 if (amdgpu_sriov_vf(adev
)) {
782 bo_va
= vm
->csa_bo_va
;
784 r
= amdgpu_vm_bo_update(adev
, bo_va
, false);
788 f
= bo_va
->last_pt_update
;
789 r
= amdgpu_sync_fence(adev
, &p
->job
->sync
, f
);
795 for (i
= 0; i
< p
->bo_list
->num_entries
; i
++) {
798 /* ignore duplicates */
799 bo
= p
->bo_list
->array
[i
].robj
;
803 bo_va
= p
->bo_list
->array
[i
].bo_va
;
807 r
= amdgpu_vm_bo_update(adev
, bo_va
, false);
811 f
= bo_va
->last_pt_update
;
812 r
= amdgpu_sync_fence(adev
, &p
->job
->sync
, f
);
819 r
= amdgpu_vm_clear_invalids(adev
, vm
, &p
->job
->sync
);
821 if (amdgpu_vm_debug
&& p
->bo_list
) {
822 /* Invalidate all BOs to test for userspace bugs */
823 for (i
= 0; i
< p
->bo_list
->num_entries
; i
++) {
824 /* ignore duplicates */
825 bo
= p
->bo_list
->array
[i
].robj
;
829 amdgpu_vm_bo_invalidate(adev
, bo
);
836 static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device
*adev
,
837 struct amdgpu_cs_parser
*p
)
839 struct amdgpu_fpriv
*fpriv
= p
->filp
->driver_priv
;
840 struct amdgpu_vm
*vm
= &fpriv
->vm
;
841 struct amdgpu_ring
*ring
= p
->job
->ring
;
844 /* Only for UVD/VCE VM emulation */
845 if (ring
->funcs
->parse_cs
) {
846 for (i
= 0; i
< p
->job
->num_ibs
; i
++) {
847 r
= amdgpu_ring_parse_cs(ring
, p
, i
);
854 p
->job
->vm_pd_addr
= amdgpu_bo_gpu_offset(vm
->page_directory
);
856 r
= amdgpu_bo_vm_update_pte(p
, vm
);
861 return amdgpu_cs_sync_rings(p
);
864 static int amdgpu_cs_ib_fill(struct amdgpu_device
*adev
,
865 struct amdgpu_cs_parser
*parser
)
867 struct amdgpu_fpriv
*fpriv
= parser
->filp
->driver_priv
;
868 struct amdgpu_vm
*vm
= &fpriv
->vm
;
872 for (i
= 0, j
= 0; i
< parser
->nchunks
&& j
< parser
->job
->num_ibs
; i
++) {
873 struct amdgpu_cs_chunk
*chunk
;
874 struct amdgpu_ib
*ib
;
875 struct drm_amdgpu_cs_chunk_ib
*chunk_ib
;
876 struct amdgpu_ring
*ring
;
878 chunk
= &parser
->chunks
[i
];
879 ib
= &parser
->job
->ibs
[j
];
880 chunk_ib
= (struct drm_amdgpu_cs_chunk_ib
*)chunk
->kdata
;
882 if (chunk
->chunk_id
!= AMDGPU_CHUNK_ID_IB
)
885 r
= amdgpu_cs_get_ring(adev
, chunk_ib
->ip_type
,
886 chunk_ib
->ip_instance
, chunk_ib
->ring
,
891 if (ib
->flags
& AMDGPU_IB_FLAG_PREAMBLE
) {
892 parser
->job
->preamble_status
|= AMDGPU_PREAMBLE_IB_PRESENT
;
893 if (!parser
->ctx
->preamble_presented
) {
894 parser
->job
->preamble_status
|= AMDGPU_PREAMBLE_IB_PRESENT_FIRST
;
895 parser
->ctx
->preamble_presented
= true;
899 if (parser
->job
->ring
&& parser
->job
->ring
!= ring
)
902 parser
->job
->ring
= ring
;
904 if (ring
->funcs
->parse_cs
) {
905 struct amdgpu_bo_va_mapping
*m
;
906 struct amdgpu_bo
*aobj
= NULL
;
910 m
= amdgpu_cs_find_mapping(parser
, chunk_ib
->va_start
,
913 DRM_ERROR("IB va_start is invalid\n");
917 if ((chunk_ib
->va_start
+ chunk_ib
->ib_bytes
) >
918 (m
->it
.last
+ 1) * AMDGPU_GPU_PAGE_SIZE
) {
919 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
923 /* the IB should be reserved at this point */
924 r
= amdgpu_bo_kmap(aobj
, (void **)&kptr
);
929 offset
= ((uint64_t)m
->it
.start
) * AMDGPU_GPU_PAGE_SIZE
;
930 kptr
+= chunk_ib
->va_start
- offset
;
932 r
= amdgpu_ib_get(adev
, vm
, chunk_ib
->ib_bytes
, ib
);
934 DRM_ERROR("Failed to get ib !\n");
938 memcpy(ib
->ptr
, kptr
, chunk_ib
->ib_bytes
);
939 amdgpu_bo_kunmap(aobj
);
941 r
= amdgpu_ib_get(adev
, vm
, 0, ib
);
943 DRM_ERROR("Failed to get ib !\n");
949 ib
->gpu_addr
= chunk_ib
->va_start
;
950 ib
->length_dw
= chunk_ib
->ib_bytes
/ 4;
951 ib
->flags
= chunk_ib
->flags
;
955 /* UVD & VCE fw doesn't support user fences */
956 if (parser
->job
->uf_addr
&& (
957 parser
->job
->ring
->funcs
->type
== AMDGPU_RING_TYPE_UVD
||
958 parser
->job
->ring
->funcs
->type
== AMDGPU_RING_TYPE_VCE
))
964 static int amdgpu_cs_dependencies(struct amdgpu_device
*adev
,
965 struct amdgpu_cs_parser
*p
)
967 struct amdgpu_fpriv
*fpriv
= p
->filp
->driver_priv
;
970 for (i
= 0; i
< p
->nchunks
; ++i
) {
971 struct drm_amdgpu_cs_chunk_dep
*deps
;
972 struct amdgpu_cs_chunk
*chunk
;
975 chunk
= &p
->chunks
[i
];
977 if (chunk
->chunk_id
!= AMDGPU_CHUNK_ID_DEPENDENCIES
)
980 deps
= (struct drm_amdgpu_cs_chunk_dep
*)chunk
->kdata
;
981 num_deps
= chunk
->length_dw
* 4 /
982 sizeof(struct drm_amdgpu_cs_chunk_dep
);
984 for (j
= 0; j
< num_deps
; ++j
) {
985 struct amdgpu_ring
*ring
;
986 struct amdgpu_ctx
*ctx
;
987 struct dma_fence
*fence
;
989 r
= amdgpu_cs_get_ring(adev
, deps
[j
].ip_type
,
991 deps
[j
].ring
, &ring
);
995 ctx
= amdgpu_ctx_get(fpriv
, deps
[j
].ctx_id
);
999 fence
= amdgpu_ctx_get_fence(ctx
, ring
,
1001 if (IS_ERR(fence
)) {
1003 amdgpu_ctx_put(ctx
);
1007 r
= amdgpu_sync_fence(adev
, &p
->job
->sync
,
1009 dma_fence_put(fence
);
1010 amdgpu_ctx_put(ctx
);
1020 static int amdgpu_cs_submit(struct amdgpu_cs_parser
*p
,
1021 union drm_amdgpu_cs
*cs
)
1023 struct amdgpu_ring
*ring
= p
->job
->ring
;
1024 struct amd_sched_entity
*entity
= &p
->ctx
->rings
[ring
->idx
].entity
;
1025 struct amdgpu_job
*job
;
1031 r
= amd_sched_job_init(&job
->base
, &ring
->sched
, entity
, p
->filp
);
1033 amdgpu_job_free(job
);
1037 job
->owner
= p
->filp
;
1038 job
->fence_ctx
= entity
->fence_context
;
1039 p
->fence
= dma_fence_get(&job
->base
.s_fence
->finished
);
1040 cs
->out
.handle
= amdgpu_ctx_add_fence(p
->ctx
, ring
, p
->fence
);
1041 job
->uf_sequence
= cs
->out
.handle
;
1042 amdgpu_job_free_resources(job
);
1044 trace_amdgpu_cs_ioctl(job
);
1045 amd_sched_entity_push_job(&job
->base
);
1050 int amdgpu_cs_ioctl(struct drm_device
*dev
, void *data
, struct drm_file
*filp
)
1052 struct amdgpu_device
*adev
= dev
->dev_private
;
1053 union drm_amdgpu_cs
*cs
= data
;
1054 struct amdgpu_cs_parser parser
= {};
1055 bool reserved_buffers
= false;
1058 if (!adev
->accel_working
)
1064 r
= amdgpu_cs_parser_init(&parser
, data
);
1066 DRM_ERROR("Failed to initialize parser !\n");
1070 r
= amdgpu_cs_parser_bos(&parser
, data
);
1073 DRM_ERROR("Not enough memory for command submission!\n");
1074 else if (r
!= -ERESTARTSYS
)
1075 DRM_ERROR("Failed to process the buffer list %d!\n", r
);
1079 reserved_buffers
= true;
1080 r
= amdgpu_cs_ib_fill(adev
, &parser
);
1084 r
= amdgpu_cs_dependencies(adev
, &parser
);
1086 DRM_ERROR("Failed in the dependencies handling %d!\n", r
);
1090 for (i
= 0; i
< parser
.job
->num_ibs
; i
++)
1091 trace_amdgpu_cs(&parser
, i
);
1093 r
= amdgpu_cs_ib_vm_chunk(adev
, &parser
);
1097 r
= amdgpu_cs_submit(&parser
, cs
);
1100 amdgpu_cs_parser_fini(&parser
, r
, reserved_buffers
);
1105 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1108 * @data: data from userspace
1109 * @filp: file private
1111 * Wait for the command submission identified by handle to finish.
1113 int amdgpu_cs_wait_ioctl(struct drm_device
*dev
, void *data
,
1114 struct drm_file
*filp
)
1116 union drm_amdgpu_wait_cs
*wait
= data
;
1117 struct amdgpu_device
*adev
= dev
->dev_private
;
1118 unsigned long timeout
= amdgpu_gem_timeout(wait
->in
.timeout
);
1119 struct amdgpu_ring
*ring
= NULL
;
1120 struct amdgpu_ctx
*ctx
;
1121 struct dma_fence
*fence
;
1124 r
= amdgpu_cs_get_ring(adev
, wait
->in
.ip_type
, wait
->in
.ip_instance
,
1125 wait
->in
.ring
, &ring
);
1129 ctx
= amdgpu_ctx_get(filp
->driver_priv
, wait
->in
.ctx_id
);
1133 fence
= amdgpu_ctx_get_fence(ctx
, ring
, wait
->in
.handle
);
1137 r
= dma_fence_wait_timeout(fence
, true, timeout
);
1138 dma_fence_put(fence
);
1142 amdgpu_ctx_put(ctx
);
1146 memset(wait
, 0, sizeof(*wait
));
1147 wait
->out
.status
= (r
== 0);
1153 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1155 * @adev: amdgpu device
1156 * @filp: file private
1157 * @user: drm_amdgpu_fence copied from user space
1159 static struct dma_fence
*amdgpu_cs_get_fence(struct amdgpu_device
*adev
,
1160 struct drm_file
*filp
,
1161 struct drm_amdgpu_fence
*user
)
1163 struct amdgpu_ring
*ring
;
1164 struct amdgpu_ctx
*ctx
;
1165 struct dma_fence
*fence
;
1168 r
= amdgpu_cs_get_ring(adev
, user
->ip_type
, user
->ip_instance
,
1173 ctx
= amdgpu_ctx_get(filp
->driver_priv
, user
->ctx_id
);
1175 return ERR_PTR(-EINVAL
);
1177 fence
= amdgpu_ctx_get_fence(ctx
, ring
, user
->seq_no
);
1178 amdgpu_ctx_put(ctx
);
1184 * amdgpu_cs_wait_all_fence - wait on all fences to signal
1186 * @adev: amdgpu device
1187 * @filp: file private
1188 * @wait: wait parameters
1189 * @fences: array of drm_amdgpu_fence
1191 static int amdgpu_cs_wait_all_fences(struct amdgpu_device
*adev
,
1192 struct drm_file
*filp
,
1193 union drm_amdgpu_wait_fences
*wait
,
1194 struct drm_amdgpu_fence
*fences
)
1196 uint32_t fence_count
= wait
->in
.fence_count
;
1200 for (i
= 0; i
< fence_count
; i
++) {
1201 struct dma_fence
*fence
;
1202 unsigned long timeout
= amdgpu_gem_timeout(wait
->in
.timeout_ns
);
1204 fence
= amdgpu_cs_get_fence(adev
, filp
, &fences
[i
]);
1206 return PTR_ERR(fence
);
1210 r
= dma_fence_wait_timeout(fence
, true, timeout
);
1218 memset(wait
, 0, sizeof(*wait
));
1219 wait
->out
.status
= (r
> 0);
1225 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1227 * @adev: amdgpu device
1228 * @filp: file private
1229 * @wait: wait parameters
1230 * @fences: array of drm_amdgpu_fence
1232 static int amdgpu_cs_wait_any_fence(struct amdgpu_device
*adev
,
1233 struct drm_file
*filp
,
1234 union drm_amdgpu_wait_fences
*wait
,
1235 struct drm_amdgpu_fence
*fences
)
1237 unsigned long timeout
= amdgpu_gem_timeout(wait
->in
.timeout_ns
);
1238 uint32_t fence_count
= wait
->in
.fence_count
;
1239 uint32_t first
= ~0;
1240 struct dma_fence
**array
;
1244 /* Prepare the fence array */
1245 array
= kcalloc(fence_count
, sizeof(struct dma_fence
*), GFP_KERNEL
);
1250 for (i
= 0; i
< fence_count
; i
++) {
1251 struct dma_fence
*fence
;
1253 fence
= amdgpu_cs_get_fence(adev
, filp
, &fences
[i
]);
1254 if (IS_ERR(fence
)) {
1256 goto err_free_fence_array
;
1259 } else { /* NULL, the fence has been already signaled */
1265 r
= dma_fence_wait_any_timeout(array
, fence_count
, true, timeout
,
1268 goto err_free_fence_array
;
1271 memset(wait
, 0, sizeof(*wait
));
1272 wait
->out
.status
= (r
> 0);
1273 wait
->out
.first_signaled
= first
;
1274 /* set return value 0 to indicate success */
1277 err_free_fence_array
:
1278 for (i
= 0; i
< fence_count
; i
++)
1279 dma_fence_put(array
[i
]);
1286 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1289 * @data: data from userspace
1290 * @filp: file private
1292 int amdgpu_cs_wait_fences_ioctl(struct drm_device
*dev
, void *data
,
1293 struct drm_file
*filp
)
1295 struct amdgpu_device
*adev
= dev
->dev_private
;
1296 union drm_amdgpu_wait_fences
*wait
= data
;
1297 uint32_t fence_count
= wait
->in
.fence_count
;
1298 struct drm_amdgpu_fence
*fences_user
;
1299 struct drm_amdgpu_fence
*fences
;
1302 /* Get the fences from userspace */
1303 fences
= kmalloc_array(fence_count
, sizeof(struct drm_amdgpu_fence
),
1308 fences_user
= (void __user
*)(unsigned long)(wait
->in
.fences
);
1309 if (copy_from_user(fences
, fences_user
,
1310 sizeof(struct drm_amdgpu_fence
) * fence_count
)) {
1312 goto err_free_fences
;
1315 if (wait
->in
.wait_all
)
1316 r
= amdgpu_cs_wait_all_fences(adev
, filp
, wait
, fences
);
1318 r
= amdgpu_cs_wait_any_fence(adev
, filp
, wait
, fences
);
1327 * amdgpu_cs_find_bo_va - find bo_va for VM address
1329 * @parser: command submission parser context
1331 * @bo: resulting BO of the mapping found
1333 * Search the buffer objects in the command submission context for a certain
1334 * virtual memory address. Returns allocation structure when found, NULL
1337 struct amdgpu_bo_va_mapping
*
1338 amdgpu_cs_find_mapping(struct amdgpu_cs_parser
*parser
,
1339 uint64_t addr
, struct amdgpu_bo
**bo
)
1341 struct amdgpu_bo_va_mapping
*mapping
;
1344 if (!parser
->bo_list
)
1347 addr
/= AMDGPU_GPU_PAGE_SIZE
;
1349 for (i
= 0; i
< parser
->bo_list
->num_entries
; i
++) {
1350 struct amdgpu_bo_list_entry
*lobj
;
1352 lobj
= &parser
->bo_list
->array
[i
];
1356 list_for_each_entry(mapping
, &lobj
->bo_va
->valids
, list
) {
1357 if (mapping
->it
.start
> addr
||
1358 addr
> mapping
->it
.last
)
1361 *bo
= lobj
->bo_va
->bo
;
1365 list_for_each_entry(mapping
, &lobj
->bo_va
->invalids
, list
) {
1366 if (mapping
->it
.start
> addr
||
1367 addr
> mapping
->it
.last
)
1370 *bo
= lobj
->bo_va
->bo
;
1379 * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM
1381 * @parser: command submission parser context
1383 * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM.
1385 int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser
*parser
)
1390 if (!parser
->bo_list
)
1393 for (i
= 0; i
< parser
->bo_list
->num_entries
; i
++) {
1394 struct amdgpu_bo
*bo
= parser
->bo_list
->array
[i
].robj
;
1396 r
= amdgpu_ttm_bind(&bo
->tbo
, &bo
->tbo
.mem
);
1400 if (bo
->flags
& AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS
)
1403 bo
->flags
|= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS
;
1404 amdgpu_ttm_placement_from_domain(bo
, bo
->allowed_domains
);
1405 r
= ttm_bo_validate(&bo
->tbo
, &bo
->placement
, false, false);