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
:
108 *out_ring
= &adev
->sdma
[ring
].ring
;
110 DRM_ERROR("only two SDMA rings are supported\n");
114 case AMDGPU_HW_IP_UVD
:
115 *out_ring
= &adev
->uvd
.ring
;
117 case AMDGPU_HW_IP_VCE
:
119 *out_ring
= &adev
->vce
.ring
[ring
];
121 DRM_ERROR("only two VCE rings are supported\n");
129 int amdgpu_cs_parser_init(struct amdgpu_cs_parser
*p
, void *data
)
131 union drm_amdgpu_cs
*cs
= data
;
132 uint64_t *chunk_array_user
;
133 uint64_t *chunk_array
= NULL
;
134 struct amdgpu_fpriv
*fpriv
= p
->filp
->driver_priv
;
138 if (!cs
->in
.num_chunks
)
141 p
->ctx
= amdgpu_ctx_get(fpriv
, cs
->in
.ctx_id
);
146 p
->bo_list
= amdgpu_bo_list_get(fpriv
, cs
->in
.bo_list_handle
);
149 INIT_LIST_HEAD(&p
->validated
);
150 chunk_array
= kcalloc(cs
->in
.num_chunks
, sizeof(uint64_t), GFP_KERNEL
);
151 if (chunk_array
== NULL
) {
156 chunk_array_user
= (uint64_t *)(unsigned long)(cs
->in
.chunks
);
157 if (copy_from_user(chunk_array
, chunk_array_user
,
158 sizeof(uint64_t)*cs
->in
.num_chunks
)) {
163 p
->nchunks
= cs
->in
.num_chunks
;
164 p
->chunks
= kcalloc(p
->nchunks
, sizeof(struct amdgpu_cs_chunk
),
166 if (p
->chunks
== NULL
) {
171 for (i
= 0; i
< p
->nchunks
; i
++) {
172 struct drm_amdgpu_cs_chunk __user
**chunk_ptr
= NULL
;
173 struct drm_amdgpu_cs_chunk user_chunk
;
174 uint32_t __user
*cdata
;
176 chunk_ptr
= (void __user
*)(unsigned long)chunk_array
[i
];
177 if (copy_from_user(&user_chunk
, chunk_ptr
,
178 sizeof(struct drm_amdgpu_cs_chunk
))) {
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
;
187 p
->chunks
[i
].user_ptr
= cdata
;
189 p
->chunks
[i
].kdata
= drm_malloc_ab(size
, sizeof(uint32_t));
190 if (p
->chunks
[i
].kdata
== NULL
) {
194 size
*= sizeof(uint32_t);
195 if (copy_from_user(p
->chunks
[i
].kdata
, cdata
, size
)) {
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 struct drm_gem_object
*gobj
;
210 struct drm_amdgpu_cs_chunk_fence
*fence_data
;
212 fence_data
= (void *)p
->chunks
[i
].kdata
;
213 handle
= fence_data
->handle
;
214 gobj
= drm_gem_object_lookup(p
->adev
->ddev
,
221 p
->uf
.bo
= gem_to_amdgpu_bo(gobj
);
222 p
->uf
.offset
= fence_data
->offset
;
229 case AMDGPU_CHUNK_ID_DEPENDENCIES
:
238 p
->ibs
= kcalloc(p
->num_ibs
, sizeof(struct amdgpu_ib
), GFP_KERNEL
);
249 /* Returns how many bytes TTM can move per IB.
251 static u64
amdgpu_cs_get_threshold_for_moves(struct amdgpu_device
*adev
)
253 u64 real_vram_size
= adev
->mc
.real_vram_size
;
254 u64 vram_usage
= atomic64_read(&adev
->vram_usage
);
256 /* This function is based on the current VRAM usage.
258 * - If all of VRAM is free, allow relocating the number of bytes that
259 * is equal to 1/4 of the size of VRAM for this IB.
261 * - If more than one half of VRAM is occupied, only allow relocating
262 * 1 MB of data for this IB.
264 * - From 0 to one half of used VRAM, the threshold decreases
279 * Note: It's a threshold, not a limit. The threshold must be crossed
280 * for buffer relocations to stop, so any buffer of an arbitrary size
281 * can be moved as long as the threshold isn't crossed before
282 * the relocation takes place. We don't want to disable buffer
283 * relocations completely.
285 * The idea is that buffers should be placed in VRAM at creation time
286 * and TTM should only do a minimum number of relocations during
287 * command submission. In practice, you need to submit at least
288 * a dozen IBs to move all buffers to VRAM if they are in GTT.
290 * Also, things can get pretty crazy under memory pressure and actual
291 * VRAM usage can change a lot, so playing safe even at 50% does
292 * consistently increase performance.
295 u64 half_vram
= real_vram_size
>> 1;
296 u64 half_free_vram
= vram_usage
>= half_vram
? 0 : half_vram
- vram_usage
;
297 u64 bytes_moved_threshold
= half_free_vram
>> 1;
298 return max(bytes_moved_threshold
, 1024*1024ull);
301 int amdgpu_cs_list_validate(struct amdgpu_cs_parser
*p
)
303 struct amdgpu_fpriv
*fpriv
= p
->filp
->driver_priv
;
304 struct amdgpu_vm
*vm
= &fpriv
->vm
;
305 struct amdgpu_device
*adev
= p
->adev
;
306 struct amdgpu_bo_list_entry
*lobj
;
307 struct list_head duplicates
;
308 struct amdgpu_bo
*bo
;
309 u64 bytes_moved
= 0, initial_bytes_moved
;
310 u64 bytes_moved_threshold
= amdgpu_cs_get_threshold_for_moves(adev
);
313 INIT_LIST_HEAD(&duplicates
);
314 r
= ttm_eu_reserve_buffers(&p
->ticket
, &p
->validated
, true, &duplicates
);
315 if (unlikely(r
!= 0)) {
319 list_for_each_entry(lobj
, &p
->validated
, tv
.head
) {
321 if (!bo
->pin_count
) {
322 u32 domain
= lobj
->prefered_domains
;
324 amdgpu_mem_type_to_domain(bo
->tbo
.mem
.mem_type
);
326 /* Check if this buffer will be moved and don't move it
327 * if we have moved too many buffers for this IB already.
329 * Note that this allows moving at least one buffer of
330 * any size, because it doesn't take the current "bo"
331 * into account. We don't want to disallow buffer moves
334 if (current_domain
!= AMDGPU_GEM_DOMAIN_CPU
&&
335 (domain
& current_domain
) == 0 && /* will be moved */
336 bytes_moved
> bytes_moved_threshold
) {
338 domain
= current_domain
;
342 amdgpu_ttm_placement_from_domain(bo
, domain
);
343 initial_bytes_moved
= atomic64_read(&adev
->num_bytes_moved
);
344 r
= ttm_bo_validate(&bo
->tbo
, &bo
->placement
, true, false);
345 bytes_moved
+= atomic64_read(&adev
->num_bytes_moved
) -
349 if (r
!= -ERESTARTSYS
&& domain
!= lobj
->allowed_domains
) {
350 domain
= lobj
->allowed_domains
;
353 ttm_eu_backoff_reservation(&p
->ticket
, &p
->validated
);
357 lobj
->bo_va
= amdgpu_vm_bo_find(vm
, bo
);
362 static int amdgpu_cs_parser_relocs(struct amdgpu_cs_parser
*p
)
364 struct amdgpu_fpriv
*fpriv
= p
->filp
->driver_priv
;
365 struct amdgpu_cs_buckets buckets
;
366 bool need_mmap_lock
= false;
370 need_mmap_lock
= p
->bo_list
->has_userptr
;
371 amdgpu_cs_buckets_init(&buckets
);
372 for (i
= 0; i
< p
->bo_list
->num_entries
; i
++)
373 amdgpu_cs_buckets_add(&buckets
, &p
->bo_list
->array
[i
].tv
.head
,
374 p
->bo_list
->array
[i
].priority
);
376 amdgpu_cs_buckets_get_list(&buckets
, &p
->validated
);
379 p
->vm_bos
= amdgpu_vm_get_bos(p
->adev
, &fpriv
->vm
,
383 down_read(¤t
->mm
->mmap_sem
);
385 r
= amdgpu_cs_list_validate(p
);
388 up_read(¤t
->mm
->mmap_sem
);
393 static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser
*p
)
395 struct amdgpu_bo_list_entry
*e
;
398 list_for_each_entry(e
, &p
->validated
, tv
.head
) {
399 struct reservation_object
*resv
= e
->robj
->tbo
.resv
;
400 r
= amdgpu_sync_resv(p
->adev
, &p
->ibs
[0].sync
, resv
, p
->filp
);
408 static int cmp_size_smaller_first(void *priv
, struct list_head
*a
,
411 struct amdgpu_bo_list_entry
*la
= list_entry(a
, struct amdgpu_bo_list_entry
, tv
.head
);
412 struct amdgpu_bo_list_entry
*lb
= list_entry(b
, struct amdgpu_bo_list_entry
, tv
.head
);
414 /* Sort A before B if A is smaller. */
415 return (int)la
->robj
->tbo
.num_pages
- (int)lb
->robj
->tbo
.num_pages
;
419 * cs_parser_fini() - clean parser states
420 * @parser: parser structure holding parsing context.
421 * @error: error number
423 * If error is set than unvalidate buffer, otherwise just free memory
424 * used by parsing context.
426 static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser
*parser
, int error
, bool backoff
)
431 /* Sort the buffer list from the smallest to largest buffer,
432 * which affects the order of buffers in the LRU list.
433 * This assures that the smallest buffers are added first
434 * to the LRU list, so they are likely to be later evicted
435 * first, instead of large buffers whose eviction is more
438 * This slightly lowers the number of bytes moved by TTM
439 * per frame under memory pressure.
441 list_sort(NULL
, &parser
->validated
, cmp_size_smaller_first
);
443 ttm_eu_fence_buffer_objects(&parser
->ticket
,
445 &parser
->ibs
[parser
->num_ibs
-1].fence
->base
);
446 } else if (backoff
) {
447 ttm_eu_backoff_reservation(&parser
->ticket
,
452 amdgpu_ctx_put(parser
->ctx
);
454 amdgpu_bo_list_put(parser
->bo_list
);
455 drm_free_large(parser
->vm_bos
);
456 for (i
= 0; i
< parser
->nchunks
; i
++)
457 drm_free_large(parser
->chunks
[i
].kdata
);
458 kfree(parser
->chunks
);
460 for (i
= 0; i
< parser
->num_ibs
; i
++)
461 amdgpu_ib_free(parser
->adev
, &parser
->ibs
[i
]);
464 drm_gem_object_unreference_unlocked(&parser
->uf
.bo
->gem_base
);
467 static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser
*p
,
468 struct amdgpu_vm
*vm
)
470 struct amdgpu_device
*adev
= p
->adev
;
471 struct amdgpu_bo_va
*bo_va
;
472 struct amdgpu_bo
*bo
;
475 r
= amdgpu_vm_update_page_directory(adev
, vm
);
479 r
= amdgpu_vm_clear_freed(adev
, vm
);
484 for (i
= 0; i
< p
->bo_list
->num_entries
; i
++) {
485 /* ignore duplicates */
486 bo
= p
->bo_list
->array
[i
].robj
;
490 bo_va
= p
->bo_list
->array
[i
].bo_va
;
494 r
= amdgpu_vm_bo_update(adev
, bo_va
, &bo
->tbo
.mem
);
498 amdgpu_sync_fence(&p
->ibs
[0].sync
, bo_va
->last_pt_update
);
502 return amdgpu_vm_clear_invalids(adev
, vm
, &p
->ibs
[0].sync
);
505 static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device
*adev
,
506 struct amdgpu_cs_parser
*parser
)
508 struct amdgpu_fpriv
*fpriv
= parser
->filp
->driver_priv
;
509 struct amdgpu_vm
*vm
= &fpriv
->vm
;
510 struct amdgpu_ring
*ring
;
513 if (parser
->num_ibs
== 0)
516 /* Only for UVD/VCE VM emulation */
517 for (i
= 0; i
< parser
->num_ibs
; i
++) {
518 ring
= parser
->ibs
[i
].ring
;
519 if (ring
->funcs
->parse_cs
) {
520 r
= amdgpu_ring_parse_cs(ring
, parser
, i
);
526 mutex_lock(&vm
->mutex
);
527 r
= amdgpu_bo_vm_update_pte(parser
, vm
);
531 amdgpu_cs_sync_rings(parser
);
533 r
= amdgpu_ib_schedule(adev
, parser
->num_ibs
, parser
->ibs
,
537 mutex_unlock(&vm
->mutex
);
541 static int amdgpu_cs_handle_lockup(struct amdgpu_device
*adev
, int r
)
544 r
= amdgpu_gpu_reset(adev
);
551 static int amdgpu_cs_ib_fill(struct amdgpu_device
*adev
,
552 struct amdgpu_cs_parser
*parser
)
554 struct amdgpu_fpriv
*fpriv
= parser
->filp
->driver_priv
;
555 struct amdgpu_vm
*vm
= &fpriv
->vm
;
559 for (i
= 0, j
= 0; i
< parser
->nchunks
&& j
< parser
->num_ibs
; i
++) {
560 struct amdgpu_cs_chunk
*chunk
;
561 struct amdgpu_ib
*ib
;
562 struct drm_amdgpu_cs_chunk_ib
*chunk_ib
;
563 struct amdgpu_ring
*ring
;
565 chunk
= &parser
->chunks
[i
];
566 ib
= &parser
->ibs
[j
];
567 chunk_ib
= (struct drm_amdgpu_cs_chunk_ib
*)chunk
->kdata
;
569 if (chunk
->chunk_id
!= AMDGPU_CHUNK_ID_IB
)
572 r
= amdgpu_cs_get_ring(adev
, chunk_ib
->ip_type
,
573 chunk_ib
->ip_instance
, chunk_ib
->ring
,
578 if (ring
->funcs
->parse_cs
) {
579 struct amdgpu_bo_va_mapping
*m
;
580 struct amdgpu_bo
*aobj
= NULL
;
584 m
= amdgpu_cs_find_mapping(parser
, chunk_ib
->va_start
,
587 DRM_ERROR("IB va_start is invalid\n");
591 if ((chunk_ib
->va_start
+ chunk_ib
->ib_bytes
) >
592 (m
->it
.last
+ 1) * AMDGPU_GPU_PAGE_SIZE
) {
593 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
597 /* the IB should be reserved at this point */
598 r
= amdgpu_bo_kmap(aobj
, (void **)&kptr
);
603 offset
= ((uint64_t)m
->it
.start
) * AMDGPU_GPU_PAGE_SIZE
;
604 kptr
+= chunk_ib
->va_start
- offset
;
606 r
= amdgpu_ib_get(ring
, NULL
, chunk_ib
->ib_bytes
, ib
);
608 DRM_ERROR("Failed to get ib !\n");
612 memcpy(ib
->ptr
, kptr
, chunk_ib
->ib_bytes
);
613 amdgpu_bo_kunmap(aobj
);
615 r
= amdgpu_ib_get(ring
, vm
, 0, ib
);
617 DRM_ERROR("Failed to get ib !\n");
621 ib
->gpu_addr
= chunk_ib
->va_start
;
624 ib
->length_dw
= chunk_ib
->ib_bytes
/ 4;
625 ib
->flags
= chunk_ib
->flags
;
626 ib
->ctx
= parser
->ctx
;
630 if (!parser
->num_ibs
)
633 /* add GDS resources to first IB */
634 if (parser
->bo_list
) {
635 struct amdgpu_bo
*gds
= parser
->bo_list
->gds_obj
;
636 struct amdgpu_bo
*gws
= parser
->bo_list
->gws_obj
;
637 struct amdgpu_bo
*oa
= parser
->bo_list
->oa_obj
;
638 struct amdgpu_ib
*ib
= &parser
->ibs
[0];
641 ib
->gds_base
= amdgpu_bo_gpu_offset(gds
);
642 ib
->gds_size
= amdgpu_bo_size(gds
);
645 ib
->gws_base
= amdgpu_bo_gpu_offset(gws
);
646 ib
->gws_size
= amdgpu_bo_size(gws
);
649 ib
->oa_base
= amdgpu_bo_gpu_offset(oa
);
650 ib
->oa_size
= amdgpu_bo_size(oa
);
654 /* wrap the last IB with user fence */
656 struct amdgpu_ib
*ib
= &parser
->ibs
[parser
->num_ibs
- 1];
658 /* UVD & VCE fw doesn't support user fences */
659 if (ib
->ring
->type
== AMDGPU_RING_TYPE_UVD
||
660 ib
->ring
->type
== AMDGPU_RING_TYPE_VCE
)
663 ib
->user
= &parser
->uf
;
669 static int amdgpu_cs_dependencies(struct amdgpu_device
*adev
,
670 struct amdgpu_cs_parser
*p
)
672 struct amdgpu_fpriv
*fpriv
= p
->filp
->driver_priv
;
673 struct amdgpu_ib
*ib
;
679 /* Add dependencies to first IB */
681 for (i
= 0; i
< p
->nchunks
; ++i
) {
682 struct drm_amdgpu_cs_chunk_dep
*deps
;
683 struct amdgpu_cs_chunk
*chunk
;
686 chunk
= &p
->chunks
[i
];
688 if (chunk
->chunk_id
!= AMDGPU_CHUNK_ID_DEPENDENCIES
)
691 deps
= (struct drm_amdgpu_cs_chunk_dep
*)chunk
->kdata
;
692 num_deps
= chunk
->length_dw
* 4 /
693 sizeof(struct drm_amdgpu_cs_chunk_dep
);
695 for (j
= 0; j
< num_deps
; ++j
) {
696 struct amdgpu_fence
*fence
;
697 struct amdgpu_ring
*ring
;
698 struct amdgpu_ctx
*ctx
;
700 r
= amdgpu_cs_get_ring(adev
, deps
[j
].ip_type
,
702 deps
[j
].ring
, &ring
);
706 ctx
= amdgpu_ctx_get(fpriv
, deps
[j
].ctx_id
);
710 r
= amdgpu_fence_recreate(ring
, p
->filp
,
718 amdgpu_sync_fence(&ib
->sync
, fence
);
719 amdgpu_fence_unref(&fence
);
727 int amdgpu_cs_ioctl(struct drm_device
*dev
, void *data
, struct drm_file
*filp
)
729 struct amdgpu_device
*adev
= dev
->dev_private
;
730 union drm_amdgpu_cs
*cs
= data
;
731 struct amdgpu_cs_parser parser
;
733 bool reserved_buffers
= false;
735 down_read(&adev
->exclusive_lock
);
736 if (!adev
->accel_working
) {
737 up_read(&adev
->exclusive_lock
);
740 /* initialize parser */
741 memset(&parser
, 0, sizeof(struct amdgpu_cs_parser
));
744 r
= amdgpu_cs_parser_init(&parser
, data
);
746 DRM_ERROR("Failed to initialize parser !\n");
747 amdgpu_cs_parser_fini(&parser
, r
, false);
748 up_read(&adev
->exclusive_lock
);
749 r
= amdgpu_cs_handle_lockup(adev
, r
);
753 r
= amdgpu_cs_parser_relocs(&parser
);
755 if (r
!= -ERESTARTSYS
) {
757 DRM_ERROR("Not enough memory for command submission!\n");
759 DRM_ERROR("Failed to process the buffer list %d!\n", r
);
764 reserved_buffers
= true;
765 r
= amdgpu_cs_ib_fill(adev
, &parser
);
769 r
= amdgpu_cs_dependencies(adev
, &parser
);
772 amdgpu_cs_parser_fini(&parser
, r
, reserved_buffers
);
773 up_read(&adev
->exclusive_lock
);
774 r
= amdgpu_cs_handle_lockup(adev
, r
);
778 for (i
= 0; i
< parser
.num_ibs
; i
++)
779 trace_amdgpu_cs(&parser
, i
);
781 r
= amdgpu_cs_ib_vm_chunk(adev
, &parser
);
786 cs
->out
.handle
= parser
.ibs
[parser
.num_ibs
- 1].fence
->seq
;
788 amdgpu_cs_parser_fini(&parser
, r
, true);
789 up_read(&adev
->exclusive_lock
);
790 r
= amdgpu_cs_handle_lockup(adev
, r
);
795 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
798 * @data: data from userspace
799 * @filp: file private
801 * Wait for the command submission identified by handle to finish.
803 int amdgpu_cs_wait_ioctl(struct drm_device
*dev
, void *data
,
804 struct drm_file
*filp
)
806 union drm_amdgpu_wait_cs
*wait
= data
;
807 struct amdgpu_device
*adev
= dev
->dev_private
;
808 unsigned long timeout
= amdgpu_gem_timeout(wait
->in
.timeout
);
809 struct amdgpu_fence
*fence
= NULL
;
810 struct amdgpu_ring
*ring
= NULL
;
811 struct amdgpu_ctx
*ctx
;
814 ctx
= amdgpu_ctx_get(filp
->driver_priv
, wait
->in
.ctx_id
);
818 r
= amdgpu_cs_get_ring(adev
, wait
->in
.ip_type
, wait
->in
.ip_instance
,
819 wait
->in
.ring
, &ring
);
825 r
= amdgpu_fence_recreate(ring
, filp
, wait
->in
.handle
, &fence
);
831 r
= fence_wait_timeout(&fence
->base
, true, timeout
);
832 amdgpu_fence_unref(&fence
);
837 memset(wait
, 0, sizeof(*wait
));
838 wait
->out
.status
= (r
== 0);
844 * amdgpu_cs_find_bo_va - find bo_va for VM address
846 * @parser: command submission parser context
848 * @bo: resulting BO of the mapping found
850 * Search the buffer objects in the command submission context for a certain
851 * virtual memory address. Returns allocation structure when found, NULL
854 struct amdgpu_bo_va_mapping
*
855 amdgpu_cs_find_mapping(struct amdgpu_cs_parser
*parser
,
856 uint64_t addr
, struct amdgpu_bo
**bo
)
858 struct amdgpu_bo_list_entry
*reloc
;
859 struct amdgpu_bo_va_mapping
*mapping
;
861 addr
/= AMDGPU_GPU_PAGE_SIZE
;
863 list_for_each_entry(reloc
, &parser
->validated
, tv
.head
) {
867 list_for_each_entry(mapping
, &reloc
->bo_va
->mappings
, list
) {
868 if (mapping
->it
.start
> addr
||
869 addr
> mapping
->it
.last
)
872 *bo
= reloc
->bo_va
->bo
;