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/list_sort.h>
29 #include <drm/amdgpu_drm.h>
31 #include "amdgpu_trace.h"
33 #define AMDGPU_CS_MAX_PRIORITY 32u
34 #define AMDGPU_CS_NUM_BUCKETS (AMDGPU_CS_MAX_PRIORITY + 1)
36 /* This is based on the bucket sort with O(n) time complexity.
37 * An item with priority "i" is added to bucket[i]. The lists are then
38 * concatenated in descending order.
40 struct amdgpu_cs_buckets
{
41 struct list_head bucket
[AMDGPU_CS_NUM_BUCKETS
];
44 static void amdgpu_cs_buckets_init(struct amdgpu_cs_buckets
*b
)
48 for (i
= 0; i
< AMDGPU_CS_NUM_BUCKETS
; i
++)
49 INIT_LIST_HEAD(&b
->bucket
[i
]);
52 static void amdgpu_cs_buckets_add(struct amdgpu_cs_buckets
*b
,
53 struct list_head
*item
, unsigned priority
)
55 /* Since buffers which appear sooner in the relocation list are
56 * likely to be used more often than buffers which appear later
57 * in the list, the sort mustn't change the ordering of buffers
58 * with the same priority, i.e. it must be stable.
60 list_add_tail(item
, &b
->bucket
[min(priority
, AMDGPU_CS_MAX_PRIORITY
)]);
63 static void amdgpu_cs_buckets_get_list(struct amdgpu_cs_buckets
*b
,
64 struct list_head
*out_list
)
68 /* Connect the sorted buckets in the output list. */
69 for (i
= 0; i
< AMDGPU_CS_NUM_BUCKETS
; i
++) {
70 list_splice(&b
->bucket
[i
], out_list
);
74 int amdgpu_cs_get_ring(struct amdgpu_device
*adev
, u32 ip_type
,
75 u32 ip_instance
, u32 ring
,
76 struct amdgpu_ring
**out_ring
)
78 /* Right now all IPs have only one instance - multiple rings. */
79 if (ip_instance
!= 0) {
80 DRM_ERROR("invalid ip instance: %d\n", ip_instance
);
86 DRM_ERROR("unknown ip type: %d\n", ip_type
);
88 case AMDGPU_HW_IP_GFX
:
89 if (ring
< adev
->gfx
.num_gfx_rings
) {
90 *out_ring
= &adev
->gfx
.gfx_ring
[ring
];
92 DRM_ERROR("only %d gfx rings are supported now\n",
93 adev
->gfx
.num_gfx_rings
);
97 case AMDGPU_HW_IP_COMPUTE
:
98 if (ring
< adev
->gfx
.num_compute_rings
) {
99 *out_ring
= &adev
->gfx
.compute_ring
[ring
];
101 DRM_ERROR("only %d compute rings are supported now\n",
102 adev
->gfx
.num_compute_rings
);
106 case AMDGPU_HW_IP_DMA
:
107 if (ring
< adev
->sdma
.num_instances
) {
108 *out_ring
= &adev
->sdma
.instance
[ring
].ring
;
110 DRM_ERROR("only %d SDMA rings are supported\n",
111 adev
->sdma
.num_instances
);
115 case AMDGPU_HW_IP_UVD
:
116 *out_ring
= &adev
->uvd
.ring
;
118 case AMDGPU_HW_IP_VCE
:
120 *out_ring
= &adev
->vce
.ring
[ring
];
122 DRM_ERROR("only two VCE rings are supported\n");
130 int amdgpu_cs_parser_init(struct amdgpu_cs_parser
*p
, void *data
)
132 union drm_amdgpu_cs
*cs
= data
;
133 uint64_t *chunk_array_user
;
134 uint64_t *chunk_array
;
135 struct amdgpu_fpriv
*fpriv
= p
->filp
->driver_priv
;
140 if (cs
->in
.num_chunks
== 0)
143 chunk_array
= kmalloc_array(cs
->in
.num_chunks
, sizeof(uint64_t), GFP_KERNEL
);
147 p
->ctx
= amdgpu_ctx_get(fpriv
, cs
->in
.ctx_id
);
153 p
->bo_list
= amdgpu_bo_list_get(fpriv
, cs
->in
.bo_list_handle
);
156 INIT_LIST_HEAD(&p
->validated
);
157 chunk_array_user
= (uint64_t __user
*)(unsigned long)(cs
->in
.chunks
);
158 if (copy_from_user(chunk_array
, chunk_array_user
,
159 sizeof(uint64_t)*cs
->in
.num_chunks
)) {
164 p
->nchunks
= cs
->in
.num_chunks
;
165 p
->chunks
= kmalloc_array(p
->nchunks
, sizeof(struct amdgpu_cs_chunk
),
172 for (i
= 0; i
< p
->nchunks
; i
++) {
173 struct drm_amdgpu_cs_chunk __user
**chunk_ptr
= NULL
;
174 struct drm_amdgpu_cs_chunk user_chunk
;
175 uint32_t __user
*cdata
;
177 chunk_ptr
= (void __user
*)(unsigned long)chunk_array
[i
];
178 if (copy_from_user(&user_chunk
, chunk_ptr
,
179 sizeof(struct drm_amdgpu_cs_chunk
))) {
182 goto free_partial_kdata
;
184 p
->chunks
[i
].chunk_id
= user_chunk
.chunk_id
;
185 p
->chunks
[i
].length_dw
= user_chunk
.length_dw
;
187 size
= p
->chunks
[i
].length_dw
;
188 cdata
= (void __user
*)(unsigned long)user_chunk
.chunk_data
;
189 p
->chunks
[i
].user_ptr
= cdata
;
191 p
->chunks
[i
].kdata
= drm_malloc_ab(size
, sizeof(uint32_t));
192 if (p
->chunks
[i
].kdata
== NULL
) {
195 goto free_partial_kdata
;
197 size
*= sizeof(uint32_t);
198 if (copy_from_user(p
->chunks
[i
].kdata
, cdata
, size
)) {
200 goto free_partial_kdata
;
203 switch (p
->chunks
[i
].chunk_id
) {
204 case AMDGPU_CHUNK_ID_IB
:
208 case AMDGPU_CHUNK_ID_FENCE
:
209 size
= sizeof(struct drm_amdgpu_cs_chunk_fence
);
210 if (p
->chunks
[i
].length_dw
* sizeof(uint32_t) >= size
) {
212 struct drm_gem_object
*gobj
;
213 struct drm_amdgpu_cs_chunk_fence
*fence_data
;
215 fence_data
= (void *)p
->chunks
[i
].kdata
;
216 handle
= fence_data
->handle
;
217 gobj
= drm_gem_object_lookup(p
->adev
->ddev
,
221 goto free_partial_kdata
;
224 p
->uf
.bo
= gem_to_amdgpu_bo(gobj
);
225 p
->uf
.offset
= fence_data
->offset
;
228 goto free_partial_kdata
;
232 case AMDGPU_CHUNK_ID_DEPENDENCIES
:
237 goto free_partial_kdata
;
242 p
->ibs
= kcalloc(p
->num_ibs
, sizeof(struct amdgpu_ib
), GFP_KERNEL
);
255 drm_free_large(p
->chunks
[i
].kdata
);
259 amdgpu_bo_list_put(p
->bo_list
);
260 amdgpu_ctx_put(p
->ctx
);
267 /* Returns how many bytes TTM can move per IB.
269 static u64
amdgpu_cs_get_threshold_for_moves(struct amdgpu_device
*adev
)
271 u64 real_vram_size
= adev
->mc
.real_vram_size
;
272 u64 vram_usage
= atomic64_read(&adev
->vram_usage
);
274 /* This function is based on the current VRAM usage.
276 * - If all of VRAM is free, allow relocating the number of bytes that
277 * is equal to 1/4 of the size of VRAM for this IB.
279 * - If more than one half of VRAM is occupied, only allow relocating
280 * 1 MB of data for this IB.
282 * - From 0 to one half of used VRAM, the threshold decreases
297 * Note: It's a threshold, not a limit. The threshold must be crossed
298 * for buffer relocations to stop, so any buffer of an arbitrary size
299 * can be moved as long as the threshold isn't crossed before
300 * the relocation takes place. We don't want to disable buffer
301 * relocations completely.
303 * The idea is that buffers should be placed in VRAM at creation time
304 * and TTM should only do a minimum number of relocations during
305 * command submission. In practice, you need to submit at least
306 * a dozen IBs to move all buffers to VRAM if they are in GTT.
308 * Also, things can get pretty crazy under memory pressure and actual
309 * VRAM usage can change a lot, so playing safe even at 50% does
310 * consistently increase performance.
313 u64 half_vram
= real_vram_size
>> 1;
314 u64 half_free_vram
= vram_usage
>= half_vram
? 0 : half_vram
- vram_usage
;
315 u64 bytes_moved_threshold
= half_free_vram
>> 1;
316 return max(bytes_moved_threshold
, 1024*1024ull);
319 int amdgpu_cs_list_validate(struct amdgpu_device
*adev
,
320 struct amdgpu_vm
*vm
,
321 struct list_head
*validated
)
323 struct amdgpu_bo_list_entry
*lobj
;
324 struct amdgpu_bo
*bo
;
325 u64 bytes_moved
= 0, initial_bytes_moved
;
326 u64 bytes_moved_threshold
= amdgpu_cs_get_threshold_for_moves(adev
);
329 list_for_each_entry(lobj
, validated
, tv
.head
) {
331 if (!bo
->pin_count
) {
332 u32 domain
= lobj
->prefered_domains
;
334 amdgpu_mem_type_to_domain(bo
->tbo
.mem
.mem_type
);
336 /* Check if this buffer will be moved and don't move it
337 * if we have moved too many buffers for this IB already.
339 * Note that this allows moving at least one buffer of
340 * any size, because it doesn't take the current "bo"
341 * into account. We don't want to disallow buffer moves
344 if ((lobj
->allowed_domains
& current_domain
) != 0 &&
345 (domain
& current_domain
) == 0 && /* will be moved */
346 bytes_moved
> bytes_moved_threshold
) {
348 domain
= current_domain
;
352 amdgpu_ttm_placement_from_domain(bo
, domain
);
353 initial_bytes_moved
= atomic64_read(&adev
->num_bytes_moved
);
354 r
= ttm_bo_validate(&bo
->tbo
, &bo
->placement
, true, false);
355 bytes_moved
+= atomic64_read(&adev
->num_bytes_moved
) -
359 if (r
!= -ERESTARTSYS
&& domain
!= lobj
->allowed_domains
) {
360 domain
= lobj
->allowed_domains
;
366 lobj
->bo_va
= amdgpu_vm_bo_find(vm
, bo
);
371 static int amdgpu_cs_parser_relocs(struct amdgpu_cs_parser
*p
)
373 struct amdgpu_fpriv
*fpriv
= p
->filp
->driver_priv
;
374 struct amdgpu_cs_buckets buckets
;
375 struct list_head duplicates
;
376 bool need_mmap_lock
= false;
380 need_mmap_lock
= p
->bo_list
->has_userptr
;
381 amdgpu_cs_buckets_init(&buckets
);
382 for (i
= 0; i
< p
->bo_list
->num_entries
; i
++)
383 amdgpu_cs_buckets_add(&buckets
, &p
->bo_list
->array
[i
].tv
.head
,
384 p
->bo_list
->array
[i
].priority
);
386 amdgpu_cs_buckets_get_list(&buckets
, &p
->validated
);
389 p
->vm_bos
= amdgpu_vm_get_bos(p
->adev
, &fpriv
->vm
,
393 down_read(¤t
->mm
->mmap_sem
);
395 INIT_LIST_HEAD(&duplicates
);
396 r
= ttm_eu_reserve_buffers(&p
->ticket
, &p
->validated
, true, &duplicates
);
397 if (unlikely(r
!= 0))
400 r
= amdgpu_cs_list_validate(p
->adev
, &fpriv
->vm
, &p
->validated
);
404 r
= amdgpu_cs_list_validate(p
->adev
, &fpriv
->vm
, &duplicates
);
408 ttm_eu_backoff_reservation(&p
->ticket
, &p
->validated
);
412 up_read(¤t
->mm
->mmap_sem
);
417 static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser
*p
)
419 struct amdgpu_bo_list_entry
*e
;
422 list_for_each_entry(e
, &p
->validated
, tv
.head
) {
423 struct reservation_object
*resv
= e
->robj
->tbo
.resv
;
424 r
= amdgpu_sync_resv(p
->adev
, &p
->ibs
[0].sync
, resv
, p
->filp
);
432 static int cmp_size_smaller_first(void *priv
, struct list_head
*a
,
435 struct amdgpu_bo_list_entry
*la
= list_entry(a
, struct amdgpu_bo_list_entry
, tv
.head
);
436 struct amdgpu_bo_list_entry
*lb
= list_entry(b
, struct amdgpu_bo_list_entry
, tv
.head
);
438 /* Sort A before B if A is smaller. */
439 return (int)la
->robj
->tbo
.num_pages
- (int)lb
->robj
->tbo
.num_pages
;
442 static void amdgpu_cs_parser_fini_early(struct amdgpu_cs_parser
*parser
, int error
, bool backoff
)
445 /* Sort the buffer list from the smallest to largest buffer,
446 * which affects the order of buffers in the LRU list.
447 * This assures that the smallest buffers are added first
448 * to the LRU list, so they are likely to be later evicted
449 * first, instead of large buffers whose eviction is more
452 * This slightly lowers the number of bytes moved by TTM
453 * per frame under memory pressure.
455 list_sort(NULL
, &parser
->validated
, cmp_size_smaller_first
);
457 ttm_eu_fence_buffer_objects(&parser
->ticket
,
459 &parser
->ibs
[parser
->num_ibs
-1].fence
->base
);
460 } else if (backoff
) {
461 ttm_eu_backoff_reservation(&parser
->ticket
,
466 static void amdgpu_cs_parser_fini_late(struct amdgpu_cs_parser
*parser
)
471 amdgpu_ctx_put(parser
->ctx
);
473 amdgpu_bo_list_put(parser
->bo_list
);
475 drm_free_large(parser
->vm_bos
);
476 for (i
= 0; i
< parser
->nchunks
; i
++)
477 drm_free_large(parser
->chunks
[i
].kdata
);
478 kfree(parser
->chunks
);
480 for (i
= 0; i
< parser
->num_ibs
; i
++)
481 amdgpu_ib_free(parser
->adev
, &parser
->ibs
[i
]);
484 drm_gem_object_unreference_unlocked(&parser
->uf
.bo
->gem_base
);
488 * cs_parser_fini() - clean parser states
489 * @parser: parser structure holding parsing context.
490 * @error: error number
492 * If error is set than unvalidate buffer, otherwise just free memory
493 * used by parsing context.
495 static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser
*parser
, int error
, bool backoff
)
497 amdgpu_cs_parser_fini_early(parser
, error
, backoff
);
498 amdgpu_cs_parser_fini_late(parser
);
501 static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser
*p
,
502 struct amdgpu_vm
*vm
)
504 struct amdgpu_device
*adev
= p
->adev
;
505 struct amdgpu_bo_va
*bo_va
;
506 struct amdgpu_bo
*bo
;
509 r
= amdgpu_vm_update_page_directory(adev
, vm
);
513 r
= amdgpu_sync_fence(adev
, &p
->ibs
[0].sync
, vm
->page_directory_fence
);
517 r
= amdgpu_vm_clear_freed(adev
, vm
);
522 for (i
= 0; i
< p
->bo_list
->num_entries
; i
++) {
525 /* ignore duplicates */
526 bo
= p
->bo_list
->array
[i
].robj
;
530 bo_va
= p
->bo_list
->array
[i
].bo_va
;
534 r
= amdgpu_vm_bo_update(adev
, bo_va
, &bo
->tbo
.mem
);
538 f
= bo_va
->last_pt_update
;
539 r
= amdgpu_sync_fence(adev
, &p
->ibs
[0].sync
, f
);
546 r
= amdgpu_vm_clear_invalids(adev
, vm
, &p
->ibs
[0].sync
);
548 if (amdgpu_vm_debug
&& p
->bo_list
) {
549 /* Invalidate all BOs to test for userspace bugs */
550 for (i
= 0; i
< p
->bo_list
->num_entries
; i
++) {
551 /* ignore duplicates */
552 bo
= p
->bo_list
->array
[i
].robj
;
556 amdgpu_vm_bo_invalidate(adev
, bo
);
563 static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device
*adev
,
564 struct amdgpu_cs_parser
*parser
)
566 struct amdgpu_fpriv
*fpriv
= parser
->filp
->driver_priv
;
567 struct amdgpu_vm
*vm
= &fpriv
->vm
;
568 struct amdgpu_ring
*ring
;
571 if (parser
->num_ibs
== 0)
574 /* Only for UVD/VCE VM emulation */
575 for (i
= 0; i
< parser
->num_ibs
; i
++) {
576 ring
= parser
->ibs
[i
].ring
;
577 if (ring
->funcs
->parse_cs
) {
578 r
= amdgpu_ring_parse_cs(ring
, parser
, i
);
584 r
= amdgpu_bo_vm_update_pte(parser
, vm
);
588 amdgpu_cs_sync_rings(parser
);
589 if (!amdgpu_enable_scheduler
)
590 r
= amdgpu_ib_schedule(adev
, parser
->num_ibs
, parser
->ibs
,
597 static int amdgpu_cs_handle_lockup(struct amdgpu_device
*adev
, int r
)
600 r
= amdgpu_gpu_reset(adev
);
607 static int amdgpu_cs_ib_fill(struct amdgpu_device
*adev
,
608 struct amdgpu_cs_parser
*parser
)
610 struct amdgpu_fpriv
*fpriv
= parser
->filp
->driver_priv
;
611 struct amdgpu_vm
*vm
= &fpriv
->vm
;
615 for (i
= 0, j
= 0; i
< parser
->nchunks
&& j
< parser
->num_ibs
; i
++) {
616 struct amdgpu_cs_chunk
*chunk
;
617 struct amdgpu_ib
*ib
;
618 struct drm_amdgpu_cs_chunk_ib
*chunk_ib
;
619 struct amdgpu_ring
*ring
;
621 chunk
= &parser
->chunks
[i
];
622 ib
= &parser
->ibs
[j
];
623 chunk_ib
= (struct drm_amdgpu_cs_chunk_ib
*)chunk
->kdata
;
625 if (chunk
->chunk_id
!= AMDGPU_CHUNK_ID_IB
)
628 r
= amdgpu_cs_get_ring(adev
, chunk_ib
->ip_type
,
629 chunk_ib
->ip_instance
, chunk_ib
->ring
,
634 if (ring
->funcs
->parse_cs
) {
635 struct amdgpu_bo_va_mapping
*m
;
636 struct amdgpu_bo
*aobj
= NULL
;
640 m
= amdgpu_cs_find_mapping(parser
, chunk_ib
->va_start
,
643 DRM_ERROR("IB va_start is invalid\n");
647 if ((chunk_ib
->va_start
+ chunk_ib
->ib_bytes
) >
648 (m
->it
.last
+ 1) * AMDGPU_GPU_PAGE_SIZE
) {
649 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
653 /* the IB should be reserved at this point */
654 r
= amdgpu_bo_kmap(aobj
, (void **)&kptr
);
659 offset
= ((uint64_t)m
->it
.start
) * AMDGPU_GPU_PAGE_SIZE
;
660 kptr
+= chunk_ib
->va_start
- offset
;
662 r
= amdgpu_ib_get(ring
, NULL
, chunk_ib
->ib_bytes
, ib
);
664 DRM_ERROR("Failed to get ib !\n");
668 memcpy(ib
->ptr
, kptr
, chunk_ib
->ib_bytes
);
669 amdgpu_bo_kunmap(aobj
);
671 r
= amdgpu_ib_get(ring
, vm
, 0, ib
);
673 DRM_ERROR("Failed to get ib !\n");
677 ib
->gpu_addr
= chunk_ib
->va_start
;
680 ib
->length_dw
= chunk_ib
->ib_bytes
/ 4;
681 ib
->flags
= chunk_ib
->flags
;
682 ib
->ctx
= parser
->ctx
;
686 if (!parser
->num_ibs
)
689 /* add GDS resources to first IB */
690 if (parser
->bo_list
) {
691 struct amdgpu_bo
*gds
= parser
->bo_list
->gds_obj
;
692 struct amdgpu_bo
*gws
= parser
->bo_list
->gws_obj
;
693 struct amdgpu_bo
*oa
= parser
->bo_list
->oa_obj
;
694 struct amdgpu_ib
*ib
= &parser
->ibs
[0];
697 ib
->gds_base
= amdgpu_bo_gpu_offset(gds
);
698 ib
->gds_size
= amdgpu_bo_size(gds
);
701 ib
->gws_base
= amdgpu_bo_gpu_offset(gws
);
702 ib
->gws_size
= amdgpu_bo_size(gws
);
705 ib
->oa_base
= amdgpu_bo_gpu_offset(oa
);
706 ib
->oa_size
= amdgpu_bo_size(oa
);
709 /* wrap the last IB with user fence */
711 struct amdgpu_ib
*ib
= &parser
->ibs
[parser
->num_ibs
- 1];
713 /* UVD & VCE fw doesn't support user fences */
714 if (ib
->ring
->type
== AMDGPU_RING_TYPE_UVD
||
715 ib
->ring
->type
== AMDGPU_RING_TYPE_VCE
)
718 ib
->user
= &parser
->uf
;
724 static int amdgpu_cs_dependencies(struct amdgpu_device
*adev
,
725 struct amdgpu_cs_parser
*p
)
727 struct amdgpu_fpriv
*fpriv
= p
->filp
->driver_priv
;
728 struct amdgpu_ib
*ib
;
734 /* Add dependencies to first IB */
736 for (i
= 0; i
< p
->nchunks
; ++i
) {
737 struct drm_amdgpu_cs_chunk_dep
*deps
;
738 struct amdgpu_cs_chunk
*chunk
;
741 chunk
= &p
->chunks
[i
];
743 if (chunk
->chunk_id
!= AMDGPU_CHUNK_ID_DEPENDENCIES
)
746 deps
= (struct drm_amdgpu_cs_chunk_dep
*)chunk
->kdata
;
747 num_deps
= chunk
->length_dw
* 4 /
748 sizeof(struct drm_amdgpu_cs_chunk_dep
);
750 for (j
= 0; j
< num_deps
; ++j
) {
751 struct amdgpu_ring
*ring
;
752 struct amdgpu_ctx
*ctx
;
755 r
= amdgpu_cs_get_ring(adev
, deps
[j
].ip_type
,
757 deps
[j
].ring
, &ring
);
761 ctx
= amdgpu_ctx_get(fpriv
, deps
[j
].ctx_id
);
765 fence
= amdgpu_ctx_get_fence(ctx
, ring
,
773 r
= amdgpu_sync_fence(adev
, &ib
->sync
, fence
);
785 static int amdgpu_cs_free_job(struct amdgpu_job
*job
)
789 for (i
= 0; i
< job
->num_ibs
; i
++)
790 amdgpu_ib_free(job
->adev
, &job
->ibs
[i
]);
793 drm_gem_object_unreference_unlocked(&job
->uf
.bo
->gem_base
);
797 int amdgpu_cs_ioctl(struct drm_device
*dev
, void *data
, struct drm_file
*filp
)
799 struct amdgpu_device
*adev
= dev
->dev_private
;
800 union drm_amdgpu_cs
*cs
= data
;
801 struct amdgpu_fpriv
*fpriv
= filp
->driver_priv
;
802 struct amdgpu_vm
*vm
= &fpriv
->vm
;
803 struct amdgpu_cs_parser parser
= {};
804 bool reserved_buffers
= false;
807 if (!adev
->accel_working
)
813 r
= amdgpu_cs_parser_init(&parser
, data
);
815 DRM_ERROR("Failed to initialize parser !\n");
816 amdgpu_cs_parser_fini(&parser
, r
, false);
817 r
= amdgpu_cs_handle_lockup(adev
, r
);
820 mutex_lock(&vm
->mutex
);
821 r
= amdgpu_cs_parser_relocs(&parser
);
823 DRM_ERROR("Not enough memory for command submission!\n");
824 else if (r
&& r
!= -ERESTARTSYS
)
825 DRM_ERROR("Failed to process the buffer list %d!\n", r
);
827 reserved_buffers
= true;
828 r
= amdgpu_cs_ib_fill(adev
, &parser
);
832 r
= amdgpu_cs_dependencies(adev
, &parser
);
834 DRM_ERROR("Failed in the dependencies handling %d!\n", r
);
840 for (i
= 0; i
< parser
.num_ibs
; i
++)
841 trace_amdgpu_cs(&parser
, i
);
843 r
= amdgpu_cs_ib_vm_chunk(adev
, &parser
);
847 if (amdgpu_enable_scheduler
&& parser
.num_ibs
) {
848 struct amdgpu_ring
* ring
= parser
.ibs
->ring
;
849 struct amd_sched_fence
*fence
;
850 struct amdgpu_job
*job
;
852 job
= kzalloc(sizeof(struct amdgpu_job
), GFP_KERNEL
);
858 job
->base
.sched
= &ring
->sched
;
859 job
->base
.s_entity
= &parser
.ctx
->rings
[ring
->idx
].entity
;
860 job
->adev
= parser
.adev
;
861 job
->ibs
= parser
.ibs
;
862 job
->num_ibs
= parser
.num_ibs
;
863 job
->owner
= parser
.filp
;
864 job
->free_job
= amdgpu_cs_free_job
;
866 if (job
->ibs
[job
->num_ibs
- 1].user
) {
868 job
->ibs
[job
->num_ibs
- 1].user
= &job
->uf
;
872 fence
= amd_sched_fence_create(job
->base
.s_entity
,
876 amdgpu_cs_free_job(job
);
880 job
->base
.s_fence
= fence
;
881 fence_get(&fence
->base
);
883 cs
->out
.handle
= amdgpu_ctx_add_fence(parser
.ctx
, ring
,
885 job
->ibs
[job
->num_ibs
- 1].sequence
= cs
->out
.handle
;
890 trace_amdgpu_cs_ioctl(job
);
891 amd_sched_entity_push_job(&job
->base
);
893 list_sort(NULL
, &parser
.validated
, cmp_size_smaller_first
);
894 ttm_eu_fence_buffer_objects(&parser
.ticket
, &parser
.validated
,
896 fence_put(&fence
->base
);
898 amdgpu_cs_parser_fini_late(&parser
);
899 mutex_unlock(&vm
->mutex
);
903 cs
->out
.handle
= parser
.ibs
[parser
.num_ibs
- 1].sequence
;
905 amdgpu_cs_parser_fini(&parser
, r
, reserved_buffers
);
906 mutex_unlock(&vm
->mutex
);
907 r
= amdgpu_cs_handle_lockup(adev
, r
);
912 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
915 * @data: data from userspace
916 * @filp: file private
918 * Wait for the command submission identified by handle to finish.
920 int amdgpu_cs_wait_ioctl(struct drm_device
*dev
, void *data
,
921 struct drm_file
*filp
)
923 union drm_amdgpu_wait_cs
*wait
= data
;
924 struct amdgpu_device
*adev
= dev
->dev_private
;
925 unsigned long timeout
= amdgpu_gem_timeout(wait
->in
.timeout
);
926 struct amdgpu_ring
*ring
= NULL
;
927 struct amdgpu_ctx
*ctx
;
931 r
= amdgpu_cs_get_ring(adev
, wait
->in
.ip_type
, wait
->in
.ip_instance
,
932 wait
->in
.ring
, &ring
);
936 ctx
= amdgpu_ctx_get(filp
->driver_priv
, wait
->in
.ctx_id
);
940 fence
= amdgpu_ctx_get_fence(ctx
, ring
, wait
->in
.handle
);
944 r
= fence_wait_timeout(fence
, true, timeout
);
953 memset(wait
, 0, sizeof(*wait
));
954 wait
->out
.status
= (r
== 0);
960 * amdgpu_cs_find_bo_va - find bo_va for VM address
962 * @parser: command submission parser context
964 * @bo: resulting BO of the mapping found
966 * Search the buffer objects in the command submission context for a certain
967 * virtual memory address. Returns allocation structure when found, NULL
970 struct amdgpu_bo_va_mapping
*
971 amdgpu_cs_find_mapping(struct amdgpu_cs_parser
*parser
,
972 uint64_t addr
, struct amdgpu_bo
**bo
)
974 struct amdgpu_bo_list_entry
*reloc
;
975 struct amdgpu_bo_va_mapping
*mapping
;
977 addr
/= AMDGPU_GPU_PAGE_SIZE
;
979 list_for_each_entry(reloc
, &parser
->validated
, tv
.head
) {
983 list_for_each_entry(mapping
, &reloc
->bo_va
->valids
, list
) {
984 if (mapping
->it
.start
> addr
||
985 addr
> mapping
->it
.last
)
988 *bo
= reloc
->bo_va
->bo
;
992 list_for_each_entry(mapping
, &reloc
->bo_va
->invalids
, list
) {
993 if (mapping
->it
.start
> addr
||
994 addr
> mapping
->it
.last
)
997 *bo
= reloc
->bo_va
->bo
;