2 * Copyright © 2008 Jérôme Glisse
3 * Copyright © 2010 Marek Olšák <maraeo@gmail.com>
4 * Copyright © 2015 Advanced Micro Devices, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
19 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * The above copyright notice and this permission notice (including the
25 * next paragraph) shall be included in all copies or substantial portions
30 * Marek Olšák <maraeo@gmail.com>
33 #include "amdgpu_cs.h"
34 #include "os/os_time.h"
37 #include "amd/common/sid.h"
39 DEBUG_GET_ONCE_BOOL_OPTION(noop
, "RADEON_NOOP", false)
43 static struct pipe_fence_handle
*
44 amdgpu_fence_create(struct amdgpu_ctx
*ctx
, unsigned ip_type
,
45 unsigned ip_instance
, unsigned ring
)
47 struct amdgpu_fence
*fence
= CALLOC_STRUCT(amdgpu_fence
);
49 fence
->reference
.count
= 1;
52 fence
->fence
.context
= ctx
->ctx
;
53 fence
->fence
.ip_type
= ip_type
;
54 fence
->fence
.ip_instance
= ip_instance
;
55 fence
->fence
.ring
= ring
;
56 fence
->submission_in_progress
= true;
57 p_atomic_inc(&ctx
->refcount
);
58 return (struct pipe_fence_handle
*)fence
;
61 static struct pipe_fence_handle
*
62 amdgpu_fence_import_sync_file(struct radeon_winsys
*rws
, int fd
)
64 struct amdgpu_winsys
*ws
= amdgpu_winsys(rws
);
65 struct amdgpu_fence
*fence
= CALLOC_STRUCT(amdgpu_fence
);
70 pipe_reference_init(&fence
->reference
, 1);
72 /* fence->ctx == NULL means that the fence is syncobj-based. */
74 /* Convert sync_file into syncobj. */
75 int r
= amdgpu_cs_create_syncobj(ws
->dev
, &fence
->syncobj
);
81 r
= amdgpu_cs_syncobj_import_sync_file(ws
->dev
, fence
->syncobj
, fd
);
83 amdgpu_cs_destroy_syncobj(ws
->dev
, fence
->syncobj
);
87 return (struct pipe_fence_handle
*)fence
;
90 static int amdgpu_fence_export_sync_file(struct radeon_winsys
*rws
,
91 struct pipe_fence_handle
*pfence
)
93 struct amdgpu_winsys
*ws
= amdgpu_winsys(rws
);
94 struct amdgpu_fence
*fence
= (struct amdgpu_fence
*)pfence
;
96 if (amdgpu_fence_is_syncobj(fence
)) {
99 /* Convert syncobj into sync_file. */
100 r
= amdgpu_cs_syncobj_export_sync_file(ws
->dev
, fence
->syncobj
, &fd
);
104 os_wait_until_zero(&fence
->submission_in_progress
, PIPE_TIMEOUT_INFINITE
);
106 /* Convert the amdgpu fence into a fence FD. */
108 if (amdgpu_cs_fence_to_handle(ws
->dev
, &fence
->fence
,
109 AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD
,
116 static void amdgpu_fence_submitted(struct pipe_fence_handle
*fence
,
118 uint64_t *user_fence_cpu_address
)
120 struct amdgpu_fence
*rfence
= (struct amdgpu_fence
*)fence
;
122 rfence
->fence
.fence
= seq_no
;
123 rfence
->user_fence_cpu_address
= user_fence_cpu_address
;
124 rfence
->submission_in_progress
= false;
127 static void amdgpu_fence_signalled(struct pipe_fence_handle
*fence
)
129 struct amdgpu_fence
*rfence
= (struct amdgpu_fence
*)fence
;
131 rfence
->signalled
= true;
132 rfence
->submission_in_progress
= false;
135 bool amdgpu_fence_wait(struct pipe_fence_handle
*fence
, uint64_t timeout
,
138 struct amdgpu_fence
*rfence
= (struct amdgpu_fence
*)fence
;
141 uint64_t *user_fence_cpu
;
144 if (rfence
->signalled
)
147 /* Handle syncobjs. */
148 if (amdgpu_fence_is_syncobj(rfence
)) {
149 /* Absolute timeouts are only be used by BO fences, which aren't
150 * backed by syncobjs.
154 if (amdgpu_cs_syncobj_wait(rfence
->ws
->dev
, &rfence
->syncobj
, 1,
158 rfence
->signalled
= true;
163 abs_timeout
= timeout
;
165 abs_timeout
= os_time_get_absolute_timeout(timeout
);
167 /* The fence might not have a number assigned if its IB is being
168 * submitted in the other thread right now. Wait until the submission
170 if (!os_wait_until_zero_abs_timeout(&rfence
->submission_in_progress
,
174 user_fence_cpu
= rfence
->user_fence_cpu_address
;
175 if (user_fence_cpu
) {
176 if (*user_fence_cpu
>= rfence
->fence
.fence
) {
177 rfence
->signalled
= true;
181 /* No timeout, just query: no need for the ioctl. */
182 if (!absolute
&& !timeout
)
186 /* Now use the libdrm query. */
187 r
= amdgpu_cs_query_fence_status(&rfence
->fence
,
189 AMDGPU_QUERY_FENCE_TIMEOUT_IS_ABSOLUTE
,
192 fprintf(stderr
, "amdgpu: amdgpu_cs_query_fence_status failed.\n");
197 /* This variable can only transition from false to true, so it doesn't
198 * matter if threads race for it. */
199 rfence
->signalled
= true;
205 static bool amdgpu_fence_wait_rel_timeout(struct radeon_winsys
*rws
,
206 struct pipe_fence_handle
*fence
,
209 return amdgpu_fence_wait(fence
, timeout
, false);
212 static struct pipe_fence_handle
*
213 amdgpu_cs_get_next_fence(struct radeon_winsys_cs
*rcs
)
215 struct amdgpu_cs
*cs
= amdgpu_cs(rcs
);
216 struct pipe_fence_handle
*fence
= NULL
;
218 if (debug_get_option_noop())
221 if (cs
->next_fence
) {
222 amdgpu_fence_reference(&fence
, cs
->next_fence
);
226 fence
= amdgpu_fence_create(cs
->ctx
,
227 cs
->csc
->ib
[IB_MAIN
].ip_type
,
228 cs
->csc
->ib
[IB_MAIN
].ip_instance
,
229 cs
->csc
->ib
[IB_MAIN
].ring
);
233 amdgpu_fence_reference(&cs
->next_fence
, fence
);
239 static struct radeon_winsys_ctx
*amdgpu_ctx_create(struct radeon_winsys
*ws
)
241 struct amdgpu_ctx
*ctx
= CALLOC_STRUCT(amdgpu_ctx
);
243 struct amdgpu_bo_alloc_request alloc_buffer
= {};
244 amdgpu_bo_handle buf_handle
;
249 ctx
->ws
= amdgpu_winsys(ws
);
251 ctx
->initial_num_total_rejected_cs
= ctx
->ws
->num_total_rejected_cs
;
253 r
= amdgpu_cs_ctx_create(ctx
->ws
->dev
, &ctx
->ctx
);
255 fprintf(stderr
, "amdgpu: amdgpu_cs_ctx_create failed. (%i)\n", r
);
259 alloc_buffer
.alloc_size
= ctx
->ws
->info
.gart_page_size
;
260 alloc_buffer
.phys_alignment
= ctx
->ws
->info
.gart_page_size
;
261 alloc_buffer
.preferred_heap
= AMDGPU_GEM_DOMAIN_GTT
;
263 r
= amdgpu_bo_alloc(ctx
->ws
->dev
, &alloc_buffer
, &buf_handle
);
265 fprintf(stderr
, "amdgpu: amdgpu_bo_alloc failed. (%i)\n", r
);
266 goto error_user_fence_alloc
;
269 r
= amdgpu_bo_cpu_map(buf_handle
, (void**)&ctx
->user_fence_cpu_address_base
);
271 fprintf(stderr
, "amdgpu: amdgpu_bo_cpu_map failed. (%i)\n", r
);
272 goto error_user_fence_map
;
275 memset(ctx
->user_fence_cpu_address_base
, 0, alloc_buffer
.alloc_size
);
276 ctx
->user_fence_bo
= buf_handle
;
278 return (struct radeon_winsys_ctx
*)ctx
;
280 error_user_fence_map
:
281 amdgpu_bo_free(buf_handle
);
282 error_user_fence_alloc
:
283 amdgpu_cs_ctx_free(ctx
->ctx
);
289 static void amdgpu_ctx_destroy(struct radeon_winsys_ctx
*rwctx
)
291 amdgpu_ctx_unref((struct amdgpu_ctx
*)rwctx
);
294 static enum pipe_reset_status
295 amdgpu_ctx_query_reset_status(struct radeon_winsys_ctx
*rwctx
)
297 struct amdgpu_ctx
*ctx
= (struct amdgpu_ctx
*)rwctx
;
298 uint32_t result
, hangs
;
301 /* Return a failure due to a rejected command submission. */
302 if (ctx
->ws
->num_total_rejected_cs
> ctx
->initial_num_total_rejected_cs
) {
303 return ctx
->num_rejected_cs
? PIPE_GUILTY_CONTEXT_RESET
:
304 PIPE_INNOCENT_CONTEXT_RESET
;
307 /* Return a failure due to a GPU hang. */
308 r
= amdgpu_cs_query_reset_state(ctx
->ctx
, &result
, &hangs
);
310 fprintf(stderr
, "amdgpu: amdgpu_cs_query_reset_state failed. (%i)\n", r
);
311 return PIPE_NO_RESET
;
315 case AMDGPU_CTX_GUILTY_RESET
:
316 return PIPE_GUILTY_CONTEXT_RESET
;
317 case AMDGPU_CTX_INNOCENT_RESET
:
318 return PIPE_INNOCENT_CONTEXT_RESET
;
319 case AMDGPU_CTX_UNKNOWN_RESET
:
320 return PIPE_UNKNOWN_CONTEXT_RESET
;
321 case AMDGPU_CTX_NO_RESET
:
323 return PIPE_NO_RESET
;
327 /* COMMAND SUBMISSION */
329 static bool amdgpu_cs_has_user_fence(struct amdgpu_cs_context
*cs
)
331 return cs
->ib
[IB_MAIN
].ip_type
!= AMDGPU_HW_IP_UVD
&&
332 cs
->ib
[IB_MAIN
].ip_type
!= AMDGPU_HW_IP_VCE
&&
333 cs
->ib
[IB_MAIN
].ip_type
!= AMDGPU_HW_IP_VCN_DEC
;
336 static bool amdgpu_cs_has_chaining(struct amdgpu_cs
*cs
)
338 return cs
->ctx
->ws
->info
.chip_class
>= CIK
&&
339 cs
->ring_type
== RING_GFX
;
342 static unsigned amdgpu_cs_epilog_dws(enum ring_type ring_type
)
344 if (ring_type
== RING_GFX
)
345 return 4; /* for chaining */
350 int amdgpu_lookup_buffer(struct amdgpu_cs_context
*cs
, struct amdgpu_winsys_bo
*bo
)
352 unsigned hash
= bo
->unique_id
& (ARRAY_SIZE(cs
->buffer_indices_hashlist
)-1);
353 int i
= cs
->buffer_indices_hashlist
[hash
];
354 struct amdgpu_cs_buffer
*buffers
;
358 buffers
= cs
->real_buffers
;
359 num_buffers
= cs
->num_real_buffers
;
360 } else if (!bo
->sparse
) {
361 buffers
= cs
->slab_buffers
;
362 num_buffers
= cs
->num_slab_buffers
;
364 buffers
= cs
->sparse_buffers
;
365 num_buffers
= cs
->num_sparse_buffers
;
368 /* not found or found */
369 if (i
< 0 || (i
< num_buffers
&& buffers
[i
].bo
== bo
))
372 /* Hash collision, look for the BO in the list of buffers linearly. */
373 for (i
= num_buffers
- 1; i
>= 0; i
--) {
374 if (buffers
[i
].bo
== bo
) {
375 /* Put this buffer in the hash list.
376 * This will prevent additional hash collisions if there are
377 * several consecutive lookup_buffer calls for the same buffer.
379 * Example: Assuming buffers A,B,C collide in the hash list,
380 * the following sequence of buffers:
381 * AAAAAAAAAAABBBBBBBBBBBBBBCCCCCCCC
382 * will collide here: ^ and here: ^,
383 * meaning that we should get very few collisions in the end. */
384 cs
->buffer_indices_hashlist
[hash
] = i
;
392 amdgpu_do_add_real_buffer(struct amdgpu_cs_context
*cs
, struct amdgpu_winsys_bo
*bo
)
394 struct amdgpu_cs_buffer
*buffer
;
397 /* New buffer, check if the backing array is large enough. */
398 if (cs
->num_real_buffers
>= cs
->max_real_buffers
) {
400 MAX2(cs
->max_real_buffers
+ 16, (unsigned)(cs
->max_real_buffers
* 1.3));
401 struct amdgpu_cs_buffer
*new_buffers
;
403 new_buffers
= MALLOC(new_max
* sizeof(*new_buffers
));
406 fprintf(stderr
, "amdgpu_do_add_buffer: allocation failed\n");
411 memcpy(new_buffers
, cs
->real_buffers
, cs
->num_real_buffers
* sizeof(*new_buffers
));
413 FREE(cs
->real_buffers
);
415 cs
->max_real_buffers
= new_max
;
416 cs
->real_buffers
= new_buffers
;
419 idx
= cs
->num_real_buffers
;
420 buffer
= &cs
->real_buffers
[idx
];
422 memset(buffer
, 0, sizeof(*buffer
));
423 amdgpu_winsys_bo_reference(&buffer
->bo
, bo
);
424 p_atomic_inc(&bo
->num_cs_references
);
425 cs
->num_real_buffers
++;
431 amdgpu_lookup_or_add_real_buffer(struct amdgpu_cs
*acs
, struct amdgpu_winsys_bo
*bo
)
433 struct amdgpu_cs_context
*cs
= acs
->csc
;
435 int idx
= amdgpu_lookup_buffer(cs
, bo
);
440 idx
= amdgpu_do_add_real_buffer(cs
, bo
);
442 hash
= bo
->unique_id
& (ARRAY_SIZE(cs
->buffer_indices_hashlist
)-1);
443 cs
->buffer_indices_hashlist
[hash
] = idx
;
445 if (bo
->initial_domain
& RADEON_DOMAIN_VRAM
)
446 acs
->main
.base
.used_vram
+= bo
->base
.size
;
447 else if (bo
->initial_domain
& RADEON_DOMAIN_GTT
)
448 acs
->main
.base
.used_gart
+= bo
->base
.size
;
453 static int amdgpu_lookup_or_add_slab_buffer(struct amdgpu_cs
*acs
,
454 struct amdgpu_winsys_bo
*bo
)
456 struct amdgpu_cs_context
*cs
= acs
->csc
;
457 struct amdgpu_cs_buffer
*buffer
;
459 int idx
= amdgpu_lookup_buffer(cs
, bo
);
465 real_idx
= amdgpu_lookup_or_add_real_buffer(acs
, bo
->u
.slab
.real
);
469 /* New buffer, check if the backing array is large enough. */
470 if (cs
->num_slab_buffers
>= cs
->max_slab_buffers
) {
472 MAX2(cs
->max_slab_buffers
+ 16, (unsigned)(cs
->max_slab_buffers
* 1.3));
473 struct amdgpu_cs_buffer
*new_buffers
;
475 new_buffers
= REALLOC(cs
->slab_buffers
,
476 cs
->max_slab_buffers
* sizeof(*new_buffers
),
477 new_max
* sizeof(*new_buffers
));
479 fprintf(stderr
, "amdgpu_lookup_or_add_slab_buffer: allocation failed\n");
483 cs
->max_slab_buffers
= new_max
;
484 cs
->slab_buffers
= new_buffers
;
487 idx
= cs
->num_slab_buffers
;
488 buffer
= &cs
->slab_buffers
[idx
];
490 memset(buffer
, 0, sizeof(*buffer
));
491 amdgpu_winsys_bo_reference(&buffer
->bo
, bo
);
492 buffer
->u
.slab
.real_idx
= real_idx
;
493 p_atomic_inc(&bo
->num_cs_references
);
494 cs
->num_slab_buffers
++;
496 hash
= bo
->unique_id
& (ARRAY_SIZE(cs
->buffer_indices_hashlist
)-1);
497 cs
->buffer_indices_hashlist
[hash
] = idx
;
502 static int amdgpu_lookup_or_add_sparse_buffer(struct amdgpu_cs
*acs
,
503 struct amdgpu_winsys_bo
*bo
)
505 struct amdgpu_cs_context
*cs
= acs
->csc
;
506 struct amdgpu_cs_buffer
*buffer
;
508 int idx
= amdgpu_lookup_buffer(cs
, bo
);
513 /* New buffer, check if the backing array is large enough. */
514 if (cs
->num_sparse_buffers
>= cs
->max_sparse_buffers
) {
516 MAX2(cs
->max_sparse_buffers
+ 16, (unsigned)(cs
->max_sparse_buffers
* 1.3));
517 struct amdgpu_cs_buffer
*new_buffers
;
519 new_buffers
= REALLOC(cs
->sparse_buffers
,
520 cs
->max_sparse_buffers
* sizeof(*new_buffers
),
521 new_max
* sizeof(*new_buffers
));
523 fprintf(stderr
, "amdgpu_lookup_or_add_sparse_buffer: allocation failed\n");
527 cs
->max_sparse_buffers
= new_max
;
528 cs
->sparse_buffers
= new_buffers
;
531 idx
= cs
->num_sparse_buffers
;
532 buffer
= &cs
->sparse_buffers
[idx
];
534 memset(buffer
, 0, sizeof(*buffer
));
535 amdgpu_winsys_bo_reference(&buffer
->bo
, bo
);
536 p_atomic_inc(&bo
->num_cs_references
);
537 cs
->num_sparse_buffers
++;
539 hash
= bo
->unique_id
& (ARRAY_SIZE(cs
->buffer_indices_hashlist
)-1);
540 cs
->buffer_indices_hashlist
[hash
] = idx
;
542 /* We delay adding the backing buffers until we really have to. However,
543 * we cannot delay accounting for memory use.
545 mtx_lock(&bo
->u
.sparse
.commit_lock
);
547 list_for_each_entry(struct amdgpu_sparse_backing
, backing
, &bo
->u
.sparse
.backing
, list
) {
548 if (bo
->initial_domain
& RADEON_DOMAIN_VRAM
)
549 acs
->main
.base
.used_vram
+= backing
->bo
->base
.size
;
550 else if (bo
->initial_domain
& RADEON_DOMAIN_GTT
)
551 acs
->main
.base
.used_gart
+= backing
->bo
->base
.size
;
554 mtx_unlock(&bo
->u
.sparse
.commit_lock
);
559 static unsigned amdgpu_cs_add_buffer(struct radeon_winsys_cs
*rcs
,
560 struct pb_buffer
*buf
,
561 enum radeon_bo_usage usage
,
562 enum radeon_bo_domain domains
,
563 enum radeon_bo_priority priority
)
565 /* Don't use the "domains" parameter. Amdgpu doesn't support changing
566 * the buffer placement during command submission.
568 struct amdgpu_cs
*acs
= amdgpu_cs(rcs
);
569 struct amdgpu_cs_context
*cs
= acs
->csc
;
570 struct amdgpu_winsys_bo
*bo
= (struct amdgpu_winsys_bo
*)buf
;
571 struct amdgpu_cs_buffer
*buffer
;
574 /* Fast exit for no-op calls.
575 * This is very effective with suballocators and linear uploaders that
576 * are outside of the winsys.
578 if (bo
== cs
->last_added_bo
&&
579 (usage
& cs
->last_added_bo_usage
) == usage
&&
580 (1ull << priority
) & cs
->last_added_bo_priority_usage
)
581 return cs
->last_added_bo_index
;
585 index
= amdgpu_lookup_or_add_slab_buffer(acs
, bo
);
589 buffer
= &cs
->slab_buffers
[index
];
590 buffer
->usage
|= usage
;
592 usage
&= ~RADEON_USAGE_SYNCHRONIZED
;
593 index
= buffer
->u
.slab
.real_idx
;
595 index
= amdgpu_lookup_or_add_real_buffer(acs
, bo
);
600 buffer
= &cs
->real_buffers
[index
];
602 index
= amdgpu_lookup_or_add_sparse_buffer(acs
, bo
);
606 buffer
= &cs
->sparse_buffers
[index
];
609 buffer
->u
.real
.priority_usage
|= 1ull << priority
;
610 buffer
->usage
|= usage
;
612 cs
->last_added_bo
= bo
;
613 cs
->last_added_bo_index
= index
;
614 cs
->last_added_bo_usage
= buffer
->usage
;
615 cs
->last_added_bo_priority_usage
= buffer
->u
.real
.priority_usage
;
619 static bool amdgpu_ib_new_buffer(struct amdgpu_winsys
*ws
, struct amdgpu_ib
*ib
)
621 struct pb_buffer
*pb
;
623 unsigned buffer_size
;
625 /* Always create a buffer that is at least as large as the maximum seen IB
626 * size, aligned to a power of two (and multiplied by 4 to reduce internal
627 * fragmentation if chaining is not available). Limit to 512k dwords, which
628 * is the largest power of two that fits into the size field of the
629 * INDIRECT_BUFFER packet.
631 if (amdgpu_cs_has_chaining(amdgpu_cs_from_ib(ib
)))
632 buffer_size
= 4 *util_next_power_of_two(ib
->max_ib_size
);
634 buffer_size
= 4 *util_next_power_of_two(4 * ib
->max_ib_size
);
636 buffer_size
= MIN2(buffer_size
, 4 * 512 * 1024);
638 switch (ib
->ib_type
) {
640 buffer_size
= MAX2(buffer_size
, 8 * 1024 * 4);
643 unreachable("unhandled IB type");
646 pb
= ws
->base
.buffer_create(&ws
->base
, buffer_size
,
647 ws
->info
.gart_page_size
,
649 RADEON_FLAG_NO_INTERPROCESS_SHARING
);
653 mapped
= ws
->base
.buffer_map(pb
, NULL
, PIPE_TRANSFER_WRITE
);
655 pb_reference(&pb
, NULL
);
659 pb_reference(&ib
->big_ib_buffer
, pb
);
660 pb_reference(&pb
, NULL
);
662 ib
->ib_mapped
= mapped
;
663 ib
->used_ib_space
= 0;
668 static unsigned amdgpu_ib_max_submit_dwords(enum ib_type ib_type
)
672 /* Smaller submits means the GPU gets busy sooner and there is less
673 * waiting for buffers and fences. Proof:
674 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
678 unreachable("bad ib_type");
682 static bool amdgpu_get_new_ib(struct radeon_winsys
*ws
, struct amdgpu_cs
*cs
,
683 enum ib_type ib_type
)
685 struct amdgpu_winsys
*aws
= (struct amdgpu_winsys
*)ws
;
686 /* Small IBs are better than big IBs, because the GPU goes idle quicker
687 * and there is less waiting for buffers and fences. Proof:
688 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
690 struct amdgpu_ib
*ib
= NULL
;
691 struct drm_amdgpu_cs_chunk_ib
*info
= &cs
->csc
->ib
[ib_type
];
692 unsigned ib_size
= 0;
697 ib_size
= 4 * 1024 * 4;
700 unreachable("unhandled IB type");
703 if (!amdgpu_cs_has_chaining(cs
)) {
704 ib_size
= MAX2(ib_size
,
705 4 * MIN2(util_next_power_of_two(ib
->max_ib_size
),
706 amdgpu_ib_max_submit_dwords(ib_type
)));
709 ib
->max_ib_size
= ib
->max_ib_size
- ib
->max_ib_size
/ 32;
711 ib
->base
.prev_dw
= 0;
712 ib
->base
.num_prev
= 0;
713 ib
->base
.current
.cdw
= 0;
714 ib
->base
.current
.buf
= NULL
;
716 /* Allocate a new buffer for IBs if the current buffer is all used. */
717 if (!ib
->big_ib_buffer
||
718 ib
->used_ib_space
+ ib_size
> ib
->big_ib_buffer
->size
) {
719 if (!amdgpu_ib_new_buffer(aws
, ib
))
723 info
->va_start
= amdgpu_winsys_bo(ib
->big_ib_buffer
)->va
+ ib
->used_ib_space
;
725 /* ib_bytes is in dwords and the conversion to bytes will be done before
727 ib
->ptr_ib_size
= &info
->ib_bytes
;
729 amdgpu_cs_add_buffer(&cs
->main
.base
, ib
->big_ib_buffer
,
730 RADEON_USAGE_READ
, 0, RADEON_PRIO_IB1
);
732 ib
->base
.current
.buf
= (uint32_t*)(ib
->ib_mapped
+ ib
->used_ib_space
);
734 ib_size
= ib
->big_ib_buffer
->size
- ib
->used_ib_space
;
735 ib
->base
.current
.max_dw
= ib_size
/ 4 - amdgpu_cs_epilog_dws(cs
->ring_type
);
739 static void amdgpu_ib_finalize(struct amdgpu_ib
*ib
)
741 *ib
->ptr_ib_size
|= ib
->base
.current
.cdw
;
742 ib
->used_ib_space
+= ib
->base
.current
.cdw
* 4;
743 ib
->max_ib_size
= MAX2(ib
->max_ib_size
, ib
->base
.prev_dw
+ ib
->base
.current
.cdw
);
746 static bool amdgpu_init_cs_context(struct amdgpu_cs_context
*cs
,
747 enum ring_type ring_type
)
751 cs
->ib
[IB_MAIN
].ip_type
= AMDGPU_HW_IP_DMA
;
755 cs
->ib
[IB_MAIN
].ip_type
= AMDGPU_HW_IP_UVD
;
759 cs
->ib
[IB_MAIN
].ip_type
= AMDGPU_HW_IP_VCE
;
763 cs
->ib
[IB_MAIN
].ip_type
= AMDGPU_HW_IP_COMPUTE
;
767 cs
->ib
[IB_MAIN
].ip_type
= AMDGPU_HW_IP_VCN_DEC
;
772 cs
->ib
[IB_MAIN
].ip_type
= AMDGPU_HW_IP_GFX
;
776 memset(cs
->buffer_indices_hashlist
, -1, sizeof(cs
->buffer_indices_hashlist
));
777 cs
->last_added_bo
= NULL
;
781 static void amdgpu_cs_context_cleanup(struct amdgpu_cs_context
*cs
)
785 for (i
= 0; i
< cs
->num_real_buffers
; i
++) {
786 p_atomic_dec(&cs
->real_buffers
[i
].bo
->num_cs_references
);
787 amdgpu_winsys_bo_reference(&cs
->real_buffers
[i
].bo
, NULL
);
789 for (i
= 0; i
< cs
->num_slab_buffers
; i
++) {
790 p_atomic_dec(&cs
->slab_buffers
[i
].bo
->num_cs_references
);
791 amdgpu_winsys_bo_reference(&cs
->slab_buffers
[i
].bo
, NULL
);
793 for (i
= 0; i
< cs
->num_sparse_buffers
; i
++) {
794 p_atomic_dec(&cs
->sparse_buffers
[i
].bo
->num_cs_references
);
795 amdgpu_winsys_bo_reference(&cs
->sparse_buffers
[i
].bo
, NULL
);
797 for (i
= 0; i
< cs
->num_fence_dependencies
; i
++)
798 amdgpu_fence_reference(&cs
->fence_dependencies
[i
], NULL
);
800 cs
->num_real_buffers
= 0;
801 cs
->num_slab_buffers
= 0;
802 cs
->num_sparse_buffers
= 0;
803 cs
->num_fence_dependencies
= 0;
804 amdgpu_fence_reference(&cs
->fence
, NULL
);
806 memset(cs
->buffer_indices_hashlist
, -1, sizeof(cs
->buffer_indices_hashlist
));
807 cs
->last_added_bo
= NULL
;
810 static void amdgpu_destroy_cs_context(struct amdgpu_cs_context
*cs
)
812 amdgpu_cs_context_cleanup(cs
);
814 FREE(cs
->real_buffers
);
816 FREE(cs
->slab_buffers
);
817 FREE(cs
->sparse_buffers
);
818 FREE(cs
->fence_dependencies
);
822 static struct radeon_winsys_cs
*
823 amdgpu_cs_create(struct radeon_winsys_ctx
*rwctx
,
824 enum ring_type ring_type
,
825 void (*flush
)(void *ctx
, unsigned flags
,
826 struct pipe_fence_handle
**fence
),
829 struct amdgpu_ctx
*ctx
= (struct amdgpu_ctx
*)rwctx
;
830 struct amdgpu_cs
*cs
;
832 cs
= CALLOC_STRUCT(amdgpu_cs
);
837 util_queue_fence_init(&cs
->flush_completed
);
840 cs
->flush_cs
= flush
;
841 cs
->flush_data
= flush_ctx
;
842 cs
->ring_type
= ring_type
;
844 struct amdgpu_cs_fence_info fence_info
;
845 fence_info
.handle
= cs
->ctx
->user_fence_bo
;
846 fence_info
.offset
= cs
->ring_type
;
847 amdgpu_cs_chunk_fence_info_to_data(&fence_info
, (void*)&cs
->fence_chunk
);
849 cs
->main
.ib_type
= IB_MAIN
;
851 if (!amdgpu_init_cs_context(&cs
->csc1
, ring_type
)) {
856 if (!amdgpu_init_cs_context(&cs
->csc2
, ring_type
)) {
857 amdgpu_destroy_cs_context(&cs
->csc1
);
862 /* Set the first submission context as current. */
866 if (!amdgpu_get_new_ib(&ctx
->ws
->base
, cs
, IB_MAIN
)) {
867 amdgpu_destroy_cs_context(&cs
->csc2
);
868 amdgpu_destroy_cs_context(&cs
->csc1
);
873 p_atomic_inc(&ctx
->ws
->num_cs
);
874 return &cs
->main
.base
;
877 static bool amdgpu_cs_validate(struct radeon_winsys_cs
*rcs
)
882 static bool amdgpu_cs_check_space(struct radeon_winsys_cs
*rcs
, unsigned dw
)
884 struct amdgpu_ib
*ib
= amdgpu_ib(rcs
);
885 struct amdgpu_cs
*cs
= amdgpu_cs_from_ib(ib
);
886 unsigned requested_size
= rcs
->prev_dw
+ rcs
->current
.cdw
+ dw
;
888 uint32_t *new_ptr_ib_size
;
890 assert(rcs
->current
.cdw
<= rcs
->current
.max_dw
);
892 if (requested_size
> amdgpu_ib_max_submit_dwords(ib
->ib_type
))
895 ib
->max_ib_size
= MAX2(ib
->max_ib_size
, requested_size
);
897 if (rcs
->current
.max_dw
- rcs
->current
.cdw
>= dw
)
900 if (!amdgpu_cs_has_chaining(cs
))
903 /* Allocate a new chunk */
904 if (rcs
->num_prev
>= rcs
->max_prev
) {
905 unsigned new_max_prev
= MAX2(1, 2 * rcs
->max_prev
);
906 struct radeon_winsys_cs_chunk
*new_prev
;
908 new_prev
= REALLOC(rcs
->prev
,
909 sizeof(*new_prev
) * rcs
->max_prev
,
910 sizeof(*new_prev
) * new_max_prev
);
914 rcs
->prev
= new_prev
;
915 rcs
->max_prev
= new_max_prev
;
918 if (!amdgpu_ib_new_buffer(cs
->ctx
->ws
, ib
))
921 assert(ib
->used_ib_space
== 0);
922 va
= amdgpu_winsys_bo(ib
->big_ib_buffer
)->va
;
924 /* This space was originally reserved. */
925 rcs
->current
.max_dw
+= 4;
926 assert(ib
->used_ib_space
+ 4 * rcs
->current
.max_dw
<= ib
->big_ib_buffer
->size
);
928 /* Pad with NOPs and add INDIRECT_BUFFER packet */
929 while ((rcs
->current
.cdw
& 7) != 4)
930 radeon_emit(rcs
, 0xffff1000); /* type3 nop packet */
932 radeon_emit(rcs
, PKT3(ib
->ib_type
== IB_MAIN
? PKT3_INDIRECT_BUFFER_CIK
933 : PKT3_INDIRECT_BUFFER_CONST
, 2, 0));
934 radeon_emit(rcs
, va
);
935 radeon_emit(rcs
, va
>> 32);
936 new_ptr_ib_size
= &rcs
->current
.buf
[rcs
->current
.cdw
];
937 radeon_emit(rcs
, S_3F2_CHAIN(1) | S_3F2_VALID(1));
939 assert((rcs
->current
.cdw
& 7) == 0);
940 assert(rcs
->current
.cdw
<= rcs
->current
.max_dw
);
942 *ib
->ptr_ib_size
|= rcs
->current
.cdw
;
943 ib
->ptr_ib_size
= new_ptr_ib_size
;
945 /* Hook up the new chunk */
946 rcs
->prev
[rcs
->num_prev
].buf
= rcs
->current
.buf
;
947 rcs
->prev
[rcs
->num_prev
].cdw
= rcs
->current
.cdw
;
948 rcs
->prev
[rcs
->num_prev
].max_dw
= rcs
->current
.cdw
; /* no modifications */
951 ib
->base
.prev_dw
+= ib
->base
.current
.cdw
;
952 ib
->base
.current
.cdw
= 0;
954 ib
->base
.current
.buf
= (uint32_t*)(ib
->ib_mapped
+ ib
->used_ib_space
);
955 ib
->base
.current
.max_dw
= ib
->big_ib_buffer
->size
/ 4 - amdgpu_cs_epilog_dws(cs
->ring_type
);
957 amdgpu_cs_add_buffer(&cs
->main
.base
, ib
->big_ib_buffer
,
958 RADEON_USAGE_READ
, 0, RADEON_PRIO_IB1
);
963 static unsigned amdgpu_cs_get_buffer_list(struct radeon_winsys_cs
*rcs
,
964 struct radeon_bo_list_item
*list
)
966 struct amdgpu_cs_context
*cs
= amdgpu_cs(rcs
)->csc
;
970 for (i
= 0; i
< cs
->num_real_buffers
; i
++) {
971 list
[i
].bo_size
= cs
->real_buffers
[i
].bo
->base
.size
;
972 list
[i
].vm_address
= cs
->real_buffers
[i
].bo
->va
;
973 list
[i
].priority_usage
= cs
->real_buffers
[i
].u
.real
.priority_usage
;
976 return cs
->num_real_buffers
;
979 static unsigned add_fence_dependency_entry(struct amdgpu_cs_context
*cs
)
981 unsigned idx
= cs
->num_fence_dependencies
++;
983 if (idx
>= cs
->max_fence_dependencies
) {
985 const unsigned increment
= 8;
987 cs
->max_fence_dependencies
= idx
+ increment
;
988 size
= cs
->max_fence_dependencies
* sizeof(cs
->fence_dependencies
[0]);
989 cs
->fence_dependencies
= realloc(cs
->fence_dependencies
, size
);
990 /* Clear the newly-allocated elements. */
991 memset(cs
->fence_dependencies
+ idx
, 0,
992 increment
* sizeof(cs
->fence_dependencies
[0]));
997 static bool is_noop_fence_dependency(struct amdgpu_cs
*acs
,
998 struct amdgpu_fence
*fence
)
1000 struct amdgpu_cs_context
*cs
= acs
->csc
;
1002 if (!amdgpu_fence_is_syncobj(fence
) &&
1003 fence
->ctx
== acs
->ctx
&&
1004 fence
->fence
.ip_type
== cs
->ib
[IB_MAIN
].ip_type
&&
1005 fence
->fence
.ip_instance
== cs
->ib
[IB_MAIN
].ip_instance
&&
1006 fence
->fence
.ring
== cs
->ib
[IB_MAIN
].ring
)
1009 return amdgpu_fence_wait((void *)fence
, 0, false);
1012 static void amdgpu_cs_add_fence_dependency(struct radeon_winsys_cs
*rws
,
1013 struct pipe_fence_handle
*pfence
)
1015 struct amdgpu_cs
*acs
= amdgpu_cs(rws
);
1016 struct amdgpu_cs_context
*cs
= acs
->csc
;
1017 struct amdgpu_fence
*fence
= (struct amdgpu_fence
*)pfence
;
1019 if (is_noop_fence_dependency(acs
, fence
))
1022 unsigned idx
= add_fence_dependency_entry(cs
);
1023 amdgpu_fence_reference(&cs
->fence_dependencies
[idx
],
1024 (struct pipe_fence_handle
*)fence
);
1027 static void amdgpu_add_bo_fence_dependencies(struct amdgpu_cs
*acs
,
1028 struct amdgpu_cs_buffer
*buffer
)
1030 struct amdgpu_cs_context
*cs
= acs
->csc
;
1031 struct amdgpu_winsys_bo
*bo
= buffer
->bo
;
1032 unsigned new_num_fences
= 0;
1034 for (unsigned j
= 0; j
< bo
->num_fences
; ++j
) {
1035 struct amdgpu_fence
*bo_fence
= (void *)bo
->fences
[j
];
1037 if (is_noop_fence_dependency(acs
, bo_fence
))
1040 amdgpu_fence_reference(&bo
->fences
[new_num_fences
], bo
->fences
[j
]);
1043 if (!(buffer
->usage
& RADEON_USAGE_SYNCHRONIZED
))
1046 unsigned idx
= add_fence_dependency_entry(cs
);
1047 amdgpu_fence_reference(&cs
->fence_dependencies
[idx
],
1048 (struct pipe_fence_handle
*)bo_fence
);
1051 for (unsigned j
= new_num_fences
; j
< bo
->num_fences
; ++j
)
1052 amdgpu_fence_reference(&bo
->fences
[j
], NULL
);
1054 bo
->num_fences
= new_num_fences
;
1057 /* Add the given list of fences to the buffer's fence list.
1059 * Must be called with the winsys bo_fence_lock held.
1061 void amdgpu_add_fences(struct amdgpu_winsys_bo
*bo
,
1062 unsigned num_fences
,
1063 struct pipe_fence_handle
**fences
)
1065 if (bo
->num_fences
+ num_fences
> bo
->max_fences
) {
1066 unsigned new_max_fences
= MAX2(bo
->num_fences
+ num_fences
, bo
->max_fences
* 2);
1067 struct pipe_fence_handle
**new_fences
=
1069 bo
->num_fences
* sizeof(*new_fences
),
1070 new_max_fences
* sizeof(*new_fences
));
1071 if (likely(new_fences
)) {
1072 bo
->fences
= new_fences
;
1073 bo
->max_fences
= new_max_fences
;
1077 fprintf(stderr
, "amdgpu_add_fences: allocation failure, dropping fence(s)\n");
1078 if (!bo
->num_fences
)
1081 bo
->num_fences
--; /* prefer to keep the most recent fence if possible */
1082 amdgpu_fence_reference(&bo
->fences
[bo
->num_fences
], NULL
);
1084 drop
= bo
->num_fences
+ num_fences
- bo
->max_fences
;
1090 for (unsigned i
= 0; i
< num_fences
; ++i
) {
1091 bo
->fences
[bo
->num_fences
] = NULL
;
1092 amdgpu_fence_reference(&bo
->fences
[bo
->num_fences
], fences
[i
]);
1097 static void amdgpu_add_fence_dependencies_bo_list(struct amdgpu_cs
*acs
,
1098 struct pipe_fence_handle
*fence
,
1099 unsigned num_buffers
,
1100 struct amdgpu_cs_buffer
*buffers
)
1102 for (unsigned i
= 0; i
< num_buffers
; i
++) {
1103 struct amdgpu_cs_buffer
*buffer
= &buffers
[i
];
1104 struct amdgpu_winsys_bo
*bo
= buffer
->bo
;
1106 amdgpu_add_bo_fence_dependencies(acs
, buffer
);
1107 p_atomic_inc(&bo
->num_active_ioctls
);
1108 amdgpu_add_fences(bo
, 1, &fence
);
1112 /* Since the kernel driver doesn't synchronize execution between different
1113 * rings automatically, we have to add fence dependencies manually.
1115 static void amdgpu_add_fence_dependencies_bo_lists(struct amdgpu_cs
*acs
)
1117 struct amdgpu_cs_context
*cs
= acs
->csc
;
1119 cs
->num_fence_dependencies
= 0;
1121 amdgpu_add_fence_dependencies_bo_list(acs
, cs
->fence
, cs
->num_real_buffers
, cs
->real_buffers
);
1122 amdgpu_add_fence_dependencies_bo_list(acs
, cs
->fence
, cs
->num_slab_buffers
, cs
->slab_buffers
);
1123 amdgpu_add_fence_dependencies_bo_list(acs
, cs
->fence
, cs
->num_sparse_buffers
, cs
->sparse_buffers
);
1126 /* Add backing of sparse buffers to the buffer list.
1128 * This is done late, during submission, to keep the buffer list short before
1129 * submit, and to avoid managing fences for the backing buffers.
1131 static bool amdgpu_add_sparse_backing_buffers(struct amdgpu_cs_context
*cs
)
1133 for (unsigned i
= 0; i
< cs
->num_sparse_buffers
; ++i
) {
1134 struct amdgpu_cs_buffer
*buffer
= &cs
->sparse_buffers
[i
];
1135 struct amdgpu_winsys_bo
*bo
= buffer
->bo
;
1137 mtx_lock(&bo
->u
.sparse
.commit_lock
);
1139 list_for_each_entry(struct amdgpu_sparse_backing
, backing
, &bo
->u
.sparse
.backing
, list
) {
1140 /* We can directly add the buffer here, because we know that each
1141 * backing buffer occurs only once.
1143 int idx
= amdgpu_do_add_real_buffer(cs
, backing
->bo
);
1145 fprintf(stderr
, "%s: failed to add buffer\n", __FUNCTION__
);
1146 mtx_unlock(&bo
->u
.sparse
.commit_lock
);
1150 cs
->real_buffers
[idx
].usage
= buffer
->usage
& ~RADEON_USAGE_SYNCHRONIZED
;
1151 cs
->real_buffers
[idx
].u
.real
.priority_usage
= buffer
->u
.real
.priority_usage
;
1152 p_atomic_inc(&backing
->bo
->num_active_ioctls
);
1155 mtx_unlock(&bo
->u
.sparse
.commit_lock
);
1161 void amdgpu_cs_submit_ib(void *job
, int thread_index
)
1163 struct amdgpu_cs
*acs
= (struct amdgpu_cs
*)job
;
1164 struct amdgpu_winsys
*ws
= acs
->ctx
->ws
;
1165 struct amdgpu_cs_context
*cs
= acs
->cst
;
1167 amdgpu_bo_list_handle bo_list
= NULL
;
1168 uint64_t seq_no
= 0;
1169 bool has_user_fence
= amdgpu_cs_has_user_fence(cs
);
1171 /* Create the buffer list.
1172 * Use a buffer list containing all allocated buffers if requested.
1174 if (ws
->debug_all_bos
) {
1175 struct amdgpu_winsys_bo
*bo
;
1176 amdgpu_bo_handle
*handles
;
1179 mtx_lock(&ws
->global_bo_list_lock
);
1181 handles
= malloc(sizeof(handles
[0]) * ws
->num_buffers
);
1183 mtx_unlock(&ws
->global_bo_list_lock
);
1184 amdgpu_cs_context_cleanup(cs
);
1185 cs
->error_code
= -ENOMEM
;
1189 LIST_FOR_EACH_ENTRY(bo
, &ws
->global_bo_list
, u
.real
.global_list_item
) {
1190 assert(num
< ws
->num_buffers
);
1191 handles
[num
++] = bo
->bo
;
1194 r
= amdgpu_bo_list_create(ws
->dev
, ws
->num_buffers
,
1195 handles
, NULL
, &bo_list
);
1197 mtx_unlock(&ws
->global_bo_list_lock
);
1199 unsigned num_handles
;
1201 if (!amdgpu_add_sparse_backing_buffers(cs
)) {
1206 if (cs
->max_real_submit
< cs
->num_real_buffers
) {
1210 cs
->handles
= MALLOC(sizeof(*cs
->handles
) * cs
->num_real_buffers
);
1211 cs
->flags
= MALLOC(sizeof(*cs
->flags
) * cs
->num_real_buffers
);
1213 if (!cs
->handles
|| !cs
->flags
) {
1214 cs
->max_real_submit
= 0;
1221 for (i
= 0; i
< cs
->num_real_buffers
; ++i
) {
1222 struct amdgpu_cs_buffer
*buffer
= &cs
->real_buffers
[i
];
1224 if (buffer
->bo
->is_local
)
1227 assert(buffer
->u
.real
.priority_usage
!= 0);
1229 cs
->handles
[num_handles
] = buffer
->bo
->bo
;
1230 cs
->flags
[num_handles
] = (util_last_bit64(buffer
->u
.real
.priority_usage
) - 1) / 4;
1234 if (acs
->ring_type
== RING_GFX
)
1235 ws
->gfx_bo_list_counter
+= cs
->num_real_buffers
;
1238 r
= amdgpu_bo_list_create(ws
->dev
, num_handles
,
1239 cs
->handles
, cs
->flags
, &bo_list
);
1247 fprintf(stderr
, "amdgpu: buffer list creation failed (%d)\n", r
);
1248 amdgpu_fence_signalled(cs
->fence
);
1253 if (acs
->ctx
->num_rejected_cs
) {
1256 struct drm_amdgpu_cs_chunk chunks
[4];
1257 unsigned num_chunks
= 0;
1259 /* Convert from dwords to bytes. */
1260 cs
->ib
[IB_MAIN
].ib_bytes
*= 4;
1263 chunks
[num_chunks
].chunk_id
= AMDGPU_CHUNK_ID_IB
;
1264 chunks
[num_chunks
].length_dw
= sizeof(struct drm_amdgpu_cs_chunk_ib
) / 4;
1265 chunks
[num_chunks
].chunk_data
= (uintptr_t)&cs
->ib
[IB_MAIN
];
1269 if (has_user_fence
) {
1270 chunks
[num_chunks
].chunk_id
= AMDGPU_CHUNK_ID_FENCE
;
1271 chunks
[num_chunks
].length_dw
= sizeof(struct drm_amdgpu_cs_chunk_fence
) / 4;
1272 chunks
[num_chunks
].chunk_data
= (uintptr_t)&acs
->fence_chunk
;
1277 unsigned num_dependencies
= cs
->num_fence_dependencies
;
1278 unsigned num_syncobj_dependencies
= 0;
1280 if (num_dependencies
) {
1281 struct drm_amdgpu_cs_chunk_dep
*dep_chunk
=
1282 alloca(num_dependencies
* sizeof(*dep_chunk
));
1285 for (unsigned i
= 0; i
< num_dependencies
; i
++) {
1286 struct amdgpu_fence
*fence
=
1287 (struct amdgpu_fence
*)cs
->fence_dependencies
[i
];
1289 if (amdgpu_fence_is_syncobj(fence
)) {
1290 num_syncobj_dependencies
++;
1294 assert(!fence
->submission_in_progress
);
1295 amdgpu_cs_chunk_fence_to_dep(&fence
->fence
, &dep_chunk
[num
++]);
1298 chunks
[num_chunks
].chunk_id
= AMDGPU_CHUNK_ID_DEPENDENCIES
;
1299 chunks
[num_chunks
].length_dw
= sizeof(dep_chunk
[0]) / 4 * num
;
1300 chunks
[num_chunks
].chunk_data
= (uintptr_t)dep_chunk
;
1304 /* Syncobj dependencies. */
1305 if (num_syncobj_dependencies
) {
1306 struct drm_amdgpu_cs_chunk_sem
*sem_chunk
=
1307 alloca(num_syncobj_dependencies
* sizeof(sem_chunk
[0]));
1310 for (unsigned i
= 0; i
< num_dependencies
; i
++) {
1311 struct amdgpu_fence
*fence
=
1312 (struct amdgpu_fence
*)cs
->fence_dependencies
[i
];
1314 if (!amdgpu_fence_is_syncobj(fence
))
1317 assert(!fence
->submission_in_progress
);
1318 sem_chunk
[num
++].handle
= fence
->syncobj
;
1321 chunks
[num_chunks
].chunk_id
= AMDGPU_CHUNK_ID_SYNCOBJ_IN
;
1322 chunks
[num_chunks
].length_dw
= sizeof(sem_chunk
[0]) / 4 * num
;
1323 chunks
[num_chunks
].chunk_data
= (uintptr_t)sem_chunk
;
1327 assert(num_chunks
<= ARRAY_SIZE(chunks
));
1329 r
= amdgpu_cs_submit_raw(ws
->dev
, acs
->ctx
->ctx
, bo_list
,
1330 num_chunks
, chunks
, &seq_no
);
1336 fprintf(stderr
, "amdgpu: Not enough memory for command submission.\n");
1337 else if (r
== -ECANCELED
)
1338 fprintf(stderr
, "amdgpu: The CS has been cancelled because the context is lost.\n");
1340 fprintf(stderr
, "amdgpu: The CS has been rejected, "
1341 "see dmesg for more information (%i).\n", r
);
1343 amdgpu_fence_signalled(cs
->fence
);
1345 acs
->ctx
->num_rejected_cs
++;
1346 ws
->num_total_rejected_cs
++;
1349 uint64_t *user_fence
= NULL
;
1352 user_fence
= acs
->ctx
->user_fence_cpu_address_base
+ acs
->ring_type
;
1353 amdgpu_fence_submitted(cs
->fence
, seq_no
, user_fence
);
1358 amdgpu_bo_list_destroy(bo_list
);
1361 for (i
= 0; i
< cs
->num_real_buffers
; i
++)
1362 p_atomic_dec(&cs
->real_buffers
[i
].bo
->num_active_ioctls
);
1363 for (i
= 0; i
< cs
->num_slab_buffers
; i
++)
1364 p_atomic_dec(&cs
->slab_buffers
[i
].bo
->num_active_ioctls
);
1365 for (i
= 0; i
< cs
->num_sparse_buffers
; i
++)
1366 p_atomic_dec(&cs
->sparse_buffers
[i
].bo
->num_active_ioctls
);
1368 amdgpu_cs_context_cleanup(cs
);
1371 /* Make sure the previous submission is completed. */
1372 void amdgpu_cs_sync_flush(struct radeon_winsys_cs
*rcs
)
1374 struct amdgpu_cs
*cs
= amdgpu_cs(rcs
);
1376 /* Wait for any pending ioctl of this CS to complete. */
1377 util_queue_fence_wait(&cs
->flush_completed
);
1380 static int amdgpu_cs_flush(struct radeon_winsys_cs
*rcs
,
1382 struct pipe_fence_handle
**fence
)
1384 struct amdgpu_cs
*cs
= amdgpu_cs(rcs
);
1385 struct amdgpu_winsys
*ws
= cs
->ctx
->ws
;
1388 rcs
->current
.max_dw
+= amdgpu_cs_epilog_dws(cs
->ring_type
);
1390 switch (cs
->ring_type
) {
1392 /* pad DMA ring to 8 DWs */
1393 if (ws
->info
.chip_class
<= SI
) {
1394 while (rcs
->current
.cdw
& 7)
1395 radeon_emit(rcs
, 0xf0000000); /* NOP packet */
1397 while (rcs
->current
.cdw
& 7)
1398 radeon_emit(rcs
, 0x00000000); /* NOP packet */
1402 /* pad GFX ring to 8 DWs to meet CP fetch alignment requirements */
1403 if (ws
->info
.gfx_ib_pad_with_type2
) {
1404 while (rcs
->current
.cdw
& 7)
1405 radeon_emit(rcs
, 0x80000000); /* type2 nop packet */
1407 while (rcs
->current
.cdw
& 7)
1408 radeon_emit(rcs
, 0xffff1000); /* type3 nop packet */
1412 while (rcs
->current
.cdw
& 15)
1413 radeon_emit(rcs
, 0x80000000); /* type2 nop packet */
1416 while (rcs
->current
.cdw
& 15)
1417 radeon_emit(rcs
, 0x81ff); /* nop packet */
1423 if (rcs
->current
.cdw
> rcs
->current
.max_dw
) {
1424 fprintf(stderr
, "amdgpu: command stream overflowed\n");
1427 /* If the CS is not empty or overflowed.... */
1428 if (likely(radeon_emitted(&cs
->main
.base
, 0) &&
1429 cs
->main
.base
.current
.cdw
<= cs
->main
.base
.current
.max_dw
&&
1430 !debug_get_option_noop())) {
1431 struct amdgpu_cs_context
*cur
= cs
->csc
;
1434 amdgpu_ib_finalize(&cs
->main
);
1436 /* Create a fence. */
1437 amdgpu_fence_reference(&cur
->fence
, NULL
);
1438 if (cs
->next_fence
) {
1439 /* just move the reference */
1440 cur
->fence
= cs
->next_fence
;
1441 cs
->next_fence
= NULL
;
1443 cur
->fence
= amdgpu_fence_create(cs
->ctx
,
1444 cur
->ib
[IB_MAIN
].ip_type
,
1445 cur
->ib
[IB_MAIN
].ip_instance
,
1446 cur
->ib
[IB_MAIN
].ring
);
1449 amdgpu_fence_reference(fence
, cur
->fence
);
1451 amdgpu_cs_sync_flush(rcs
);
1455 * This fence must be held until the submission is queued to ensure
1456 * that the order of fence dependency updates matches the order of
1459 mtx_lock(&ws
->bo_fence_lock
);
1460 amdgpu_add_fence_dependencies_bo_lists(cs
);
1462 /* Swap command streams. "cst" is going to be submitted. */
1467 util_queue_add_job(&ws
->cs_queue
, cs
, &cs
->flush_completed
,
1468 amdgpu_cs_submit_ib
, NULL
);
1469 /* The submission has been queued, unlock the fence now. */
1470 mtx_unlock(&ws
->bo_fence_lock
);
1472 if (!(flags
& RADEON_FLUSH_ASYNC
)) {
1473 amdgpu_cs_sync_flush(rcs
);
1474 error_code
= cur
->error_code
;
1477 amdgpu_cs_context_cleanup(cs
->csc
);
1480 amdgpu_get_new_ib(&ws
->base
, cs
, IB_MAIN
);
1482 cs
->main
.base
.used_gart
= 0;
1483 cs
->main
.base
.used_vram
= 0;
1485 if (cs
->ring_type
== RING_GFX
)
1487 else if (cs
->ring_type
== RING_DMA
)
1493 static void amdgpu_cs_destroy(struct radeon_winsys_cs
*rcs
)
1495 struct amdgpu_cs
*cs
= amdgpu_cs(rcs
);
1497 amdgpu_cs_sync_flush(rcs
);
1498 util_queue_fence_destroy(&cs
->flush_completed
);
1499 p_atomic_dec(&cs
->ctx
->ws
->num_cs
);
1500 pb_reference(&cs
->main
.big_ib_buffer
, NULL
);
1501 FREE(cs
->main
.base
.prev
);
1502 amdgpu_destroy_cs_context(&cs
->csc1
);
1503 amdgpu_destroy_cs_context(&cs
->csc2
);
1504 amdgpu_fence_reference(&cs
->next_fence
, NULL
);
1508 static bool amdgpu_bo_is_referenced(struct radeon_winsys_cs
*rcs
,
1509 struct pb_buffer
*_buf
,
1510 enum radeon_bo_usage usage
)
1512 struct amdgpu_cs
*cs
= amdgpu_cs(rcs
);
1513 struct amdgpu_winsys_bo
*bo
= (struct amdgpu_winsys_bo
*)_buf
;
1515 return amdgpu_bo_is_referenced_by_cs_with_usage(cs
, bo
, usage
);
1518 void amdgpu_cs_init_functions(struct amdgpu_winsys
*ws
)
1520 ws
->base
.ctx_create
= amdgpu_ctx_create
;
1521 ws
->base
.ctx_destroy
= amdgpu_ctx_destroy
;
1522 ws
->base
.ctx_query_reset_status
= amdgpu_ctx_query_reset_status
;
1523 ws
->base
.cs_create
= amdgpu_cs_create
;
1524 ws
->base
.cs_destroy
= amdgpu_cs_destroy
;
1525 ws
->base
.cs_add_buffer
= amdgpu_cs_add_buffer
;
1526 ws
->base
.cs_validate
= amdgpu_cs_validate
;
1527 ws
->base
.cs_check_space
= amdgpu_cs_check_space
;
1528 ws
->base
.cs_get_buffer_list
= amdgpu_cs_get_buffer_list
;
1529 ws
->base
.cs_flush
= amdgpu_cs_flush
;
1530 ws
->base
.cs_get_next_fence
= amdgpu_cs_get_next_fence
;
1531 ws
->base
.cs_is_buffer_referenced
= amdgpu_bo_is_referenced
;
1532 ws
->base
.cs_sync_flush
= amdgpu_cs_sync_flush
;
1533 ws
->base
.cs_add_fence_dependency
= amdgpu_cs_add_fence_dependency
;
1534 ws
->base
.fence_wait
= amdgpu_fence_wait_rel_timeout
;
1535 ws
->base
.fence_reference
= amdgpu_fence_reference
;
1536 ws
->base
.fence_import_sync_file
= amdgpu_fence_import_sync_file
;
1537 ws
->base
.fence_export_sync_file
= amdgpu_fence_export_sync_file
;