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"
36 #include <amdgpu_drm.h>
38 #include "amd/common/sid.h"
40 DEBUG_GET_ONCE_BOOL_OPTION(noop
, "RADEON_NOOP", false)
44 static struct pipe_fence_handle
*
45 amdgpu_fence_create(struct amdgpu_ctx
*ctx
, unsigned ip_type
,
46 unsigned ip_instance
, unsigned ring
)
48 struct amdgpu_fence
*fence
= CALLOC_STRUCT(amdgpu_fence
);
50 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 void amdgpu_fence_submitted(struct pipe_fence_handle
*fence
,
62 struct amdgpu_cs_request
* request
,
63 uint64_t *user_fence_cpu_address
)
65 struct amdgpu_fence
*rfence
= (struct amdgpu_fence
*)fence
;
67 rfence
->fence
.fence
= request
->seq_no
;
68 rfence
->user_fence_cpu_address
= user_fence_cpu_address
;
69 rfence
->submission_in_progress
= false;
72 static void amdgpu_fence_signalled(struct pipe_fence_handle
*fence
)
74 struct amdgpu_fence
*rfence
= (struct amdgpu_fence
*)fence
;
76 rfence
->signalled
= true;
77 rfence
->submission_in_progress
= false;
80 bool amdgpu_fence_wait(struct pipe_fence_handle
*fence
, uint64_t timeout
,
83 struct amdgpu_fence
*rfence
= (struct amdgpu_fence
*)fence
;
86 uint64_t *user_fence_cpu
;
89 if (rfence
->signalled
)
93 abs_timeout
= timeout
;
95 abs_timeout
= os_time_get_absolute_timeout(timeout
);
97 /* The fence might not have a number assigned if its IB is being
98 * submitted in the other thread right now. Wait until the submission
100 if (!os_wait_until_zero_abs_timeout(&rfence
->submission_in_progress
,
104 user_fence_cpu
= rfence
->user_fence_cpu_address
;
105 if (user_fence_cpu
) {
106 if (*user_fence_cpu
>= rfence
->fence
.fence
) {
107 rfence
->signalled
= true;
111 /* No timeout, just query: no need for the ioctl. */
112 if (!absolute
&& !timeout
)
116 /* Now use the libdrm query. */
117 r
= amdgpu_cs_query_fence_status(&rfence
->fence
,
119 AMDGPU_QUERY_FENCE_TIMEOUT_IS_ABSOLUTE
,
122 fprintf(stderr
, "amdgpu: amdgpu_cs_query_fence_status failed.\n");
127 /* This variable can only transition from false to true, so it doesn't
128 * matter if threads race for it. */
129 rfence
->signalled
= true;
135 static bool amdgpu_fence_wait_rel_timeout(struct radeon_winsys
*rws
,
136 struct pipe_fence_handle
*fence
,
139 return amdgpu_fence_wait(fence
, timeout
, false);
142 static struct pipe_fence_handle
*
143 amdgpu_cs_get_next_fence(struct radeon_winsys_cs
*rcs
)
145 struct amdgpu_cs
*cs
= amdgpu_cs(rcs
);
146 struct pipe_fence_handle
*fence
= NULL
;
148 if (debug_get_option_noop())
151 if (cs
->next_fence
) {
152 amdgpu_fence_reference(&fence
, cs
->next_fence
);
156 fence
= amdgpu_fence_create(cs
->ctx
,
157 cs
->csc
->request
.ip_type
,
158 cs
->csc
->request
.ip_instance
,
159 cs
->csc
->request
.ring
);
163 amdgpu_fence_reference(&cs
->next_fence
, fence
);
169 static struct radeon_winsys_ctx
*amdgpu_ctx_create(struct radeon_winsys
*ws
)
171 struct amdgpu_ctx
*ctx
= CALLOC_STRUCT(amdgpu_ctx
);
173 struct amdgpu_bo_alloc_request alloc_buffer
= {};
174 amdgpu_bo_handle buf_handle
;
179 ctx
->ws
= amdgpu_winsys(ws
);
181 ctx
->initial_num_total_rejected_cs
= ctx
->ws
->num_total_rejected_cs
;
183 r
= amdgpu_cs_ctx_create(ctx
->ws
->dev
, &ctx
->ctx
);
185 fprintf(stderr
, "amdgpu: amdgpu_cs_ctx_create failed. (%i)\n", r
);
189 alloc_buffer
.alloc_size
= ctx
->ws
->info
.gart_page_size
;
190 alloc_buffer
.phys_alignment
= ctx
->ws
->info
.gart_page_size
;
191 alloc_buffer
.preferred_heap
= AMDGPU_GEM_DOMAIN_GTT
;
193 r
= amdgpu_bo_alloc(ctx
->ws
->dev
, &alloc_buffer
, &buf_handle
);
195 fprintf(stderr
, "amdgpu: amdgpu_bo_alloc failed. (%i)\n", r
);
196 goto error_user_fence_alloc
;
199 r
= amdgpu_bo_cpu_map(buf_handle
, (void**)&ctx
->user_fence_cpu_address_base
);
201 fprintf(stderr
, "amdgpu: amdgpu_bo_cpu_map failed. (%i)\n", r
);
202 goto error_user_fence_map
;
205 memset(ctx
->user_fence_cpu_address_base
, 0, alloc_buffer
.alloc_size
);
206 ctx
->user_fence_bo
= buf_handle
;
208 return (struct radeon_winsys_ctx
*)ctx
;
210 error_user_fence_map
:
211 amdgpu_bo_free(buf_handle
);
212 error_user_fence_alloc
:
213 amdgpu_cs_ctx_free(ctx
->ctx
);
219 static void amdgpu_ctx_destroy(struct radeon_winsys_ctx
*rwctx
)
221 amdgpu_ctx_unref((struct amdgpu_ctx
*)rwctx
);
224 static enum pipe_reset_status
225 amdgpu_ctx_query_reset_status(struct radeon_winsys_ctx
*rwctx
)
227 struct amdgpu_ctx
*ctx
= (struct amdgpu_ctx
*)rwctx
;
228 uint32_t result
, hangs
;
231 /* Return a failure due to a rejected command submission. */
232 if (ctx
->ws
->num_total_rejected_cs
> ctx
->initial_num_total_rejected_cs
) {
233 return ctx
->num_rejected_cs
? PIPE_GUILTY_CONTEXT_RESET
:
234 PIPE_INNOCENT_CONTEXT_RESET
;
237 /* Return a failure due to a GPU hang. */
238 r
= amdgpu_cs_query_reset_state(ctx
->ctx
, &result
, &hangs
);
240 fprintf(stderr
, "amdgpu: amdgpu_cs_query_reset_state failed. (%i)\n", r
);
241 return PIPE_NO_RESET
;
245 case AMDGPU_CTX_GUILTY_RESET
:
246 return PIPE_GUILTY_CONTEXT_RESET
;
247 case AMDGPU_CTX_INNOCENT_RESET
:
248 return PIPE_INNOCENT_CONTEXT_RESET
;
249 case AMDGPU_CTX_UNKNOWN_RESET
:
250 return PIPE_UNKNOWN_CONTEXT_RESET
;
251 case AMDGPU_CTX_NO_RESET
:
253 return PIPE_NO_RESET
;
257 /* COMMAND SUBMISSION */
259 static bool amdgpu_cs_has_user_fence(struct amdgpu_cs_context
*cs
)
261 return cs
->request
.ip_type
!= AMDGPU_HW_IP_UVD
&&
262 cs
->request
.ip_type
!= AMDGPU_HW_IP_VCE
;
265 static bool amdgpu_cs_has_chaining(struct amdgpu_cs
*cs
)
267 return cs
->ctx
->ws
->info
.chip_class
>= CIK
&&
268 cs
->ring_type
== RING_GFX
;
271 static unsigned amdgpu_cs_epilog_dws(enum ring_type ring_type
)
273 if (ring_type
== RING_GFX
)
274 return 4; /* for chaining */
279 int amdgpu_lookup_buffer(struct amdgpu_cs_context
*cs
, struct amdgpu_winsys_bo
*bo
)
281 unsigned hash
= bo
->unique_id
& (ARRAY_SIZE(cs
->buffer_indices_hashlist
)-1);
282 int i
= cs
->buffer_indices_hashlist
[hash
];
283 struct amdgpu_cs_buffer
*buffers
;
287 buffers
= cs
->real_buffers
;
288 num_buffers
= cs
->num_real_buffers
;
290 buffers
= cs
->slab_buffers
;
291 num_buffers
= cs
->num_slab_buffers
;
294 /* not found or found */
295 if (i
< 0 || (i
< num_buffers
&& buffers
[i
].bo
== bo
))
298 /* Hash collision, look for the BO in the list of buffers linearly. */
299 for (i
= num_buffers
- 1; i
>= 0; i
--) {
300 if (buffers
[i
].bo
== bo
) {
301 /* Put this buffer in the hash list.
302 * This will prevent additional hash collisions if there are
303 * several consecutive lookup_buffer calls for the same buffer.
305 * Example: Assuming buffers A,B,C collide in the hash list,
306 * the following sequence of buffers:
307 * AAAAAAAAAAABBBBBBBBBBBBBBCCCCCCCC
308 * will collide here: ^ and here: ^,
309 * meaning that we should get very few collisions in the end. */
310 cs
->buffer_indices_hashlist
[hash
] = i
;
318 amdgpu_lookup_or_add_real_buffer(struct amdgpu_cs
*acs
, struct amdgpu_winsys_bo
*bo
)
320 struct amdgpu_cs_context
*cs
= acs
->csc
;
321 struct amdgpu_cs_buffer
*buffer
;
323 int idx
= amdgpu_lookup_buffer(cs
, bo
);
328 /* New buffer, check if the backing array is large enough. */
329 if (cs
->num_real_buffers
>= cs
->max_real_buffers
) {
331 MAX2(cs
->max_real_buffers
+ 16, (unsigned)(cs
->max_real_buffers
* 1.3));
332 struct amdgpu_cs_buffer
*new_buffers
;
333 amdgpu_bo_handle
*new_handles
;
336 new_buffers
= MALLOC(new_max
* sizeof(*new_buffers
));
337 new_handles
= MALLOC(new_max
* sizeof(*new_handles
));
338 new_flags
= MALLOC(new_max
* sizeof(*new_flags
));
340 if (!new_buffers
|| !new_handles
|| !new_flags
) {
341 fprintf(stderr
, "amdgpu_lookup_or_add_buffer: allocation failed\n");
348 memcpy(new_buffers
, cs
->real_buffers
, cs
->num_real_buffers
* sizeof(*new_buffers
));
349 memcpy(new_handles
, cs
->handles
, cs
->num_real_buffers
* sizeof(*new_handles
));
350 memcpy(new_flags
, cs
->flags
, cs
->num_real_buffers
* sizeof(*new_flags
));
352 FREE(cs
->real_buffers
);
356 cs
->max_real_buffers
= new_max
;
357 cs
->real_buffers
= new_buffers
;
358 cs
->handles
= new_handles
;
359 cs
->flags
= new_flags
;
362 idx
= cs
->num_real_buffers
;
363 buffer
= &cs
->real_buffers
[idx
];
365 memset(buffer
, 0, sizeof(*buffer
));
366 amdgpu_winsys_bo_reference(&buffer
->bo
, bo
);
367 cs
->handles
[idx
] = bo
->bo
;
369 p_atomic_inc(&bo
->num_cs_references
);
370 cs
->num_real_buffers
++;
372 hash
= bo
->unique_id
& (ARRAY_SIZE(cs
->buffer_indices_hashlist
)-1);
373 cs
->buffer_indices_hashlist
[hash
] = idx
;
375 if (bo
->initial_domain
& RADEON_DOMAIN_VRAM
)
376 acs
->main
.base
.used_vram
+= bo
->base
.size
;
377 else if (bo
->initial_domain
& RADEON_DOMAIN_GTT
)
378 acs
->main
.base
.used_gart
+= bo
->base
.size
;
383 static int amdgpu_lookup_or_add_slab_buffer(struct amdgpu_cs
*acs
,
384 struct amdgpu_winsys_bo
*bo
)
386 struct amdgpu_cs_context
*cs
= acs
->csc
;
387 struct amdgpu_cs_buffer
*buffer
;
389 int idx
= amdgpu_lookup_buffer(cs
, bo
);
395 real_idx
= amdgpu_lookup_or_add_real_buffer(acs
, bo
->u
.slab
.real
);
399 /* New buffer, check if the backing array is large enough. */
400 if (cs
->num_slab_buffers
>= cs
->max_slab_buffers
) {
402 MAX2(cs
->max_slab_buffers
+ 16, (unsigned)(cs
->max_slab_buffers
* 1.3));
403 struct amdgpu_cs_buffer
*new_buffers
;
405 new_buffers
= REALLOC(cs
->slab_buffers
,
406 cs
->max_slab_buffers
* sizeof(*new_buffers
),
407 new_max
* sizeof(*new_buffers
));
409 fprintf(stderr
, "amdgpu_lookup_or_add_slab_buffer: allocation failed\n");
413 cs
->max_slab_buffers
= new_max
;
414 cs
->slab_buffers
= new_buffers
;
417 idx
= cs
->num_slab_buffers
;
418 buffer
= &cs
->slab_buffers
[idx
];
420 memset(buffer
, 0, sizeof(*buffer
));
421 amdgpu_winsys_bo_reference(&buffer
->bo
, bo
);
422 buffer
->u
.slab
.real_idx
= real_idx
;
423 p_atomic_inc(&bo
->num_cs_references
);
424 cs
->num_slab_buffers
++;
426 hash
= bo
->unique_id
& (ARRAY_SIZE(cs
->buffer_indices_hashlist
)-1);
427 cs
->buffer_indices_hashlist
[hash
] = idx
;
432 static unsigned amdgpu_cs_add_buffer(struct radeon_winsys_cs
*rcs
,
433 struct pb_buffer
*buf
,
434 enum radeon_bo_usage usage
,
435 enum radeon_bo_domain domains
,
436 enum radeon_bo_priority priority
)
438 /* Don't use the "domains" parameter. Amdgpu doesn't support changing
439 * the buffer placement during command submission.
441 struct amdgpu_cs
*acs
= amdgpu_cs(rcs
);
442 struct amdgpu_cs_context
*cs
= acs
->csc
;
443 struct amdgpu_winsys_bo
*bo
= (struct amdgpu_winsys_bo
*)buf
;
444 struct amdgpu_cs_buffer
*buffer
;
448 index
= amdgpu_lookup_or_add_slab_buffer(acs
, bo
);
452 buffer
= &cs
->slab_buffers
[index
];
453 buffer
->usage
|= usage
;
455 usage
&= ~RADEON_USAGE_SYNCHRONIZED
;
456 index
= buffer
->u
.slab
.real_idx
;
458 index
= amdgpu_lookup_or_add_real_buffer(acs
, bo
);
463 buffer
= &cs
->real_buffers
[index
];
464 buffer
->u
.real
.priority_usage
|= 1llu << priority
;
465 buffer
->usage
|= usage
;
466 cs
->flags
[index
] = MAX2(cs
->flags
[index
], priority
/ 4);
470 static bool amdgpu_ib_new_buffer(struct amdgpu_winsys
*ws
, struct amdgpu_ib
*ib
)
472 struct pb_buffer
*pb
;
474 unsigned buffer_size
;
476 /* Always create a buffer that is at least as large as the maximum seen IB
477 * size, aligned to a power of two (and multiplied by 4 to reduce internal
478 * fragmentation if chaining is not available). Limit to 512k dwords, which
479 * is the largest power of two that fits into the size field of the
480 * INDIRECT_BUFFER packet.
482 if (amdgpu_cs_has_chaining(amdgpu_cs_from_ib(ib
)))
483 buffer_size
= 4 *util_next_power_of_two(ib
->max_ib_size
);
485 buffer_size
= 4 *util_next_power_of_two(4 * ib
->max_ib_size
);
487 buffer_size
= MIN2(buffer_size
, 4 * 512 * 1024);
489 switch (ib
->ib_type
) {
490 case IB_CONST_PREAMBLE
:
491 buffer_size
= MAX2(buffer_size
, 4 * 1024);
494 buffer_size
= MAX2(buffer_size
, 16 * 1024 * 4);
497 buffer_size
= MAX2(buffer_size
, 8 * 1024 * 4);
500 unreachable("unhandled IB type");
503 pb
= ws
->base
.buffer_create(&ws
->base
, buffer_size
,
504 ws
->info
.gart_page_size
,
506 RADEON_FLAG_CPU_ACCESS
);
510 mapped
= ws
->base
.buffer_map(pb
, NULL
, PIPE_TRANSFER_WRITE
);
512 pb_reference(&pb
, NULL
);
516 pb_reference(&ib
->big_ib_buffer
, pb
);
517 pb_reference(&pb
, NULL
);
519 ib
->ib_mapped
= mapped
;
520 ib
->used_ib_space
= 0;
525 static unsigned amdgpu_ib_max_submit_dwords(enum ib_type ib_type
)
529 /* Smaller submits means the GPU gets busy sooner and there is less
530 * waiting for buffers and fences. Proof:
531 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
534 case IB_CONST_PREAMBLE
:
536 /* There isn't really any reason to limit CE IB size beyond the natural
537 * limit implied by the main IB, except perhaps GTT size. Just return
538 * an extremely large value that we never get anywhere close to.
540 return 16 * 1024 * 1024;
542 unreachable("bad ib_type");
546 static bool amdgpu_get_new_ib(struct radeon_winsys
*ws
, struct amdgpu_cs
*cs
,
547 enum ib_type ib_type
)
549 struct amdgpu_winsys
*aws
= (struct amdgpu_winsys
*)ws
;
550 /* Small IBs are better than big IBs, because the GPU goes idle quicker
551 * and there is less waiting for buffers and fences. Proof:
552 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
554 struct amdgpu_ib
*ib
= NULL
;
555 struct amdgpu_cs_ib_info
*info
= &cs
->csc
->ib
[ib_type
];
556 unsigned ib_size
= 0;
559 case IB_CONST_PREAMBLE
:
560 ib
= &cs
->const_preamble_ib
;
565 ib_size
= 8 * 1024 * 4;
569 ib_size
= 4 * 1024 * 4;
572 unreachable("unhandled IB type");
575 if (!amdgpu_cs_has_chaining(cs
)) {
576 ib_size
= MAX2(ib_size
,
577 4 * MIN2(util_next_power_of_two(ib
->max_ib_size
),
578 amdgpu_ib_max_submit_dwords(ib_type
)));
581 ib
->max_ib_size
= ib
->max_ib_size
- ib
->max_ib_size
/ 32;
583 ib
->base
.prev_dw
= 0;
584 ib
->base
.num_prev
= 0;
585 ib
->base
.current
.cdw
= 0;
586 ib
->base
.current
.buf
= NULL
;
588 /* Allocate a new buffer for IBs if the current buffer is all used. */
589 if (!ib
->big_ib_buffer
||
590 ib
->used_ib_space
+ ib_size
> ib
->big_ib_buffer
->size
) {
591 if (!amdgpu_ib_new_buffer(aws
, ib
))
595 info
->ib_mc_address
= amdgpu_winsys_bo(ib
->big_ib_buffer
)->va
+
598 ib
->ptr_ib_size
= &info
->size
;
600 amdgpu_cs_add_buffer(&cs
->main
.base
, ib
->big_ib_buffer
,
601 RADEON_USAGE_READ
, 0, RADEON_PRIO_IB1
);
603 ib
->base
.current
.buf
= (uint32_t*)(ib
->ib_mapped
+ ib
->used_ib_space
);
605 ib_size
= ib
->big_ib_buffer
->size
- ib
->used_ib_space
;
606 ib
->base
.current
.max_dw
= ib_size
/ 4 - amdgpu_cs_epilog_dws(cs
->ring_type
);
610 static void amdgpu_ib_finalize(struct amdgpu_ib
*ib
)
612 *ib
->ptr_ib_size
|= ib
->base
.current
.cdw
;
613 ib
->used_ib_space
+= ib
->base
.current
.cdw
* 4;
614 ib
->max_ib_size
= MAX2(ib
->max_ib_size
, ib
->base
.prev_dw
+ ib
->base
.current
.cdw
);
617 static bool amdgpu_init_cs_context(struct amdgpu_cs_context
*cs
,
618 enum ring_type ring_type
)
624 cs
->request
.ip_type
= AMDGPU_HW_IP_DMA
;
628 cs
->request
.ip_type
= AMDGPU_HW_IP_UVD
;
632 cs
->request
.ip_type
= AMDGPU_HW_IP_VCE
;
636 cs
->request
.ip_type
= AMDGPU_HW_IP_COMPUTE
;
641 cs
->request
.ip_type
= AMDGPU_HW_IP_GFX
;
645 for (i
= 0; i
< ARRAY_SIZE(cs
->buffer_indices_hashlist
); i
++) {
646 cs
->buffer_indices_hashlist
[i
] = -1;
649 cs
->request
.number_of_ibs
= 1;
650 cs
->request
.ibs
= &cs
->ib
[IB_MAIN
];
652 cs
->ib
[IB_CONST
].flags
= AMDGPU_IB_FLAG_CE
;
653 cs
->ib
[IB_CONST_PREAMBLE
].flags
= AMDGPU_IB_FLAG_CE
|
654 AMDGPU_IB_FLAG_PREAMBLE
;
659 static void amdgpu_cs_context_cleanup(struct amdgpu_cs_context
*cs
)
663 for (i
= 0; i
< cs
->num_real_buffers
; i
++) {
664 p_atomic_dec(&cs
->real_buffers
[i
].bo
->num_cs_references
);
665 amdgpu_winsys_bo_reference(&cs
->real_buffers
[i
].bo
, NULL
);
667 for (i
= 0; i
< cs
->num_slab_buffers
; i
++) {
668 p_atomic_dec(&cs
->slab_buffers
[i
].bo
->num_cs_references
);
669 amdgpu_winsys_bo_reference(&cs
->slab_buffers
[i
].bo
, NULL
);
672 cs
->num_real_buffers
= 0;
673 cs
->num_slab_buffers
= 0;
674 amdgpu_fence_reference(&cs
->fence
, NULL
);
676 for (i
= 0; i
< ARRAY_SIZE(cs
->buffer_indices_hashlist
); i
++) {
677 cs
->buffer_indices_hashlist
[i
] = -1;
681 static void amdgpu_destroy_cs_context(struct amdgpu_cs_context
*cs
)
683 amdgpu_cs_context_cleanup(cs
);
685 FREE(cs
->real_buffers
);
687 FREE(cs
->slab_buffers
);
688 FREE(cs
->request
.dependencies
);
692 static struct radeon_winsys_cs
*
693 amdgpu_cs_create(struct radeon_winsys_ctx
*rwctx
,
694 enum ring_type ring_type
,
695 void (*flush
)(void *ctx
, unsigned flags
,
696 struct pipe_fence_handle
**fence
),
699 struct amdgpu_ctx
*ctx
= (struct amdgpu_ctx
*)rwctx
;
700 struct amdgpu_cs
*cs
;
702 cs
= CALLOC_STRUCT(amdgpu_cs
);
707 util_queue_fence_init(&cs
->flush_completed
);
710 cs
->flush_cs
= flush
;
711 cs
->flush_data
= flush_ctx
;
712 cs
->ring_type
= ring_type
;
714 cs
->main
.ib_type
= IB_MAIN
;
715 cs
->const_ib
.ib_type
= IB_CONST
;
716 cs
->const_preamble_ib
.ib_type
= IB_CONST_PREAMBLE
;
718 if (!amdgpu_init_cs_context(&cs
->csc1
, ring_type
)) {
723 if (!amdgpu_init_cs_context(&cs
->csc2
, ring_type
)) {
724 amdgpu_destroy_cs_context(&cs
->csc1
);
729 /* Set the first submission context as current. */
733 if (!amdgpu_get_new_ib(&ctx
->ws
->base
, cs
, IB_MAIN
)) {
734 amdgpu_destroy_cs_context(&cs
->csc2
);
735 amdgpu_destroy_cs_context(&cs
->csc1
);
740 p_atomic_inc(&ctx
->ws
->num_cs
);
741 return &cs
->main
.base
;
744 static struct radeon_winsys_cs
*
745 amdgpu_cs_add_const_ib(struct radeon_winsys_cs
*rcs
)
747 struct amdgpu_cs
*cs
= (struct amdgpu_cs
*)rcs
;
748 struct amdgpu_winsys
*ws
= cs
->ctx
->ws
;
750 /* only one const IB can be added */
751 if (cs
->ring_type
!= RING_GFX
|| cs
->const_ib
.ib_mapped
)
754 if (!amdgpu_get_new_ib(&ws
->base
, cs
, IB_CONST
))
757 cs
->csc
->request
.number_of_ibs
= 2;
758 cs
->csc
->request
.ibs
= &cs
->csc
->ib
[IB_CONST
];
760 cs
->cst
->request
.number_of_ibs
= 2;
761 cs
->cst
->request
.ibs
= &cs
->cst
->ib
[IB_CONST
];
763 return &cs
->const_ib
.base
;
766 static struct radeon_winsys_cs
*
767 amdgpu_cs_add_const_preamble_ib(struct radeon_winsys_cs
*rcs
)
769 struct amdgpu_cs
*cs
= (struct amdgpu_cs
*)rcs
;
770 struct amdgpu_winsys
*ws
= cs
->ctx
->ws
;
772 /* only one const preamble IB can be added and only when the const IB has
773 * also been mapped */
774 if (cs
->ring_type
!= RING_GFX
|| !cs
->const_ib
.ib_mapped
||
775 cs
->const_preamble_ib
.ib_mapped
)
778 if (!amdgpu_get_new_ib(&ws
->base
, cs
, IB_CONST_PREAMBLE
))
781 cs
->csc
->request
.number_of_ibs
= 3;
782 cs
->csc
->request
.ibs
= &cs
->csc
->ib
[IB_CONST_PREAMBLE
];
784 cs
->cst
->request
.number_of_ibs
= 3;
785 cs
->cst
->request
.ibs
= &cs
->cst
->ib
[IB_CONST_PREAMBLE
];
787 return &cs
->const_preamble_ib
.base
;
790 static bool amdgpu_cs_validate(struct radeon_winsys_cs
*rcs
)
795 static bool amdgpu_cs_check_space(struct radeon_winsys_cs
*rcs
, unsigned dw
)
797 struct amdgpu_ib
*ib
= amdgpu_ib(rcs
);
798 struct amdgpu_cs
*cs
= amdgpu_cs_from_ib(ib
);
799 unsigned requested_size
= rcs
->prev_dw
+ rcs
->current
.cdw
+ dw
;
801 uint32_t *new_ptr_ib_size
;
803 assert(rcs
->current
.cdw
<= rcs
->current
.max_dw
);
805 if (requested_size
> amdgpu_ib_max_submit_dwords(ib
->ib_type
))
808 ib
->max_ib_size
= MAX2(ib
->max_ib_size
, requested_size
);
810 if (rcs
->current
.max_dw
- rcs
->current
.cdw
>= dw
)
813 if (!amdgpu_cs_has_chaining(cs
))
816 /* Allocate a new chunk */
817 if (rcs
->num_prev
>= rcs
->max_prev
) {
818 unsigned new_max_prev
= MAX2(1, 2 * rcs
->max_prev
);
819 struct radeon_winsys_cs_chunk
*new_prev
;
821 new_prev
= REALLOC(rcs
->prev
,
822 sizeof(*new_prev
) * rcs
->max_prev
,
823 sizeof(*new_prev
) * new_max_prev
);
827 rcs
->prev
= new_prev
;
828 rcs
->max_prev
= new_max_prev
;
831 if (!amdgpu_ib_new_buffer(cs
->ctx
->ws
, ib
))
834 assert(ib
->used_ib_space
== 0);
835 va
= amdgpu_winsys_bo(ib
->big_ib_buffer
)->va
;
837 /* This space was originally reserved. */
838 rcs
->current
.max_dw
+= 4;
839 assert(ib
->used_ib_space
+ 4 * rcs
->current
.max_dw
<= ib
->big_ib_buffer
->size
);
841 /* Pad with NOPs and add INDIRECT_BUFFER packet */
842 while ((rcs
->current
.cdw
& 7) != 4)
843 radeon_emit(rcs
, 0xffff1000); /* type3 nop packet */
845 radeon_emit(rcs
, PKT3(ib
->ib_type
== IB_MAIN
? PKT3_INDIRECT_BUFFER_CIK
846 : PKT3_INDIRECT_BUFFER_CONST
, 2, 0));
847 radeon_emit(rcs
, va
);
848 radeon_emit(rcs
, va
>> 32);
849 new_ptr_ib_size
= &rcs
->current
.buf
[rcs
->current
.cdw
];
850 radeon_emit(rcs
, S_3F2_CHAIN(1) | S_3F2_VALID(1));
852 assert((rcs
->current
.cdw
& 7) == 0);
853 assert(rcs
->current
.cdw
<= rcs
->current
.max_dw
);
855 *ib
->ptr_ib_size
|= rcs
->current
.cdw
;
856 ib
->ptr_ib_size
= new_ptr_ib_size
;
858 /* Hook up the new chunk */
859 rcs
->prev
[rcs
->num_prev
].buf
= rcs
->current
.buf
;
860 rcs
->prev
[rcs
->num_prev
].cdw
= rcs
->current
.cdw
;
861 rcs
->prev
[rcs
->num_prev
].max_dw
= rcs
->current
.cdw
; /* no modifications */
864 ib
->base
.prev_dw
+= ib
->base
.current
.cdw
;
865 ib
->base
.current
.cdw
= 0;
867 ib
->base
.current
.buf
= (uint32_t*)(ib
->ib_mapped
+ ib
->used_ib_space
);
868 ib
->base
.current
.max_dw
= ib
->big_ib_buffer
->size
/ 4 - amdgpu_cs_epilog_dws(cs
->ring_type
);
870 amdgpu_cs_add_buffer(&cs
->main
.base
, ib
->big_ib_buffer
,
871 RADEON_USAGE_READ
, 0, RADEON_PRIO_IB1
);
876 static unsigned amdgpu_cs_get_buffer_list(struct radeon_winsys_cs
*rcs
,
877 struct radeon_bo_list_item
*list
)
879 struct amdgpu_cs_context
*cs
= amdgpu_cs(rcs
)->csc
;
883 for (i
= 0; i
< cs
->num_real_buffers
; i
++) {
884 list
[i
].bo_size
= cs
->real_buffers
[i
].bo
->base
.size
;
885 list
[i
].vm_address
= cs
->real_buffers
[i
].bo
->va
;
886 list
[i
].priority_usage
= cs
->real_buffers
[i
].u
.real
.priority_usage
;
889 return cs
->num_real_buffers
;
892 DEBUG_GET_ONCE_BOOL_OPTION(all_bos
, "RADEON_ALL_BOS", false)
894 static void amdgpu_add_fence_dependency(struct amdgpu_cs
*acs
,
895 struct amdgpu_cs_buffer
*buffer
)
897 struct amdgpu_cs_context
*cs
= acs
->csc
;
898 struct amdgpu_winsys_bo
*bo
= buffer
->bo
;
899 struct amdgpu_cs_fence
*dep
;
900 unsigned new_num_fences
= 0;
902 for (unsigned j
= 0; j
< bo
->num_fences
; ++j
) {
903 struct amdgpu_fence
*bo_fence
= (void *)bo
->fences
[j
];
906 if (bo_fence
->ctx
== acs
->ctx
&&
907 bo_fence
->fence
.ip_type
== cs
->request
.ip_type
&&
908 bo_fence
->fence
.ip_instance
== cs
->request
.ip_instance
&&
909 bo_fence
->fence
.ring
== cs
->request
.ring
)
912 if (amdgpu_fence_wait((void *)bo_fence
, 0, false))
915 amdgpu_fence_reference(&bo
->fences
[new_num_fences
], bo
->fences
[j
]);
918 if (!(buffer
->usage
& RADEON_USAGE_SYNCHRONIZED
))
921 if (bo_fence
->submission_in_progress
)
922 os_wait_until_zero(&bo_fence
->submission_in_progress
,
923 PIPE_TIMEOUT_INFINITE
);
925 idx
= cs
->request
.number_of_dependencies
++;
926 if (idx
>= cs
->max_dependencies
) {
929 cs
->max_dependencies
= idx
+ 8;
930 size
= cs
->max_dependencies
* sizeof(struct amdgpu_cs_fence
);
931 cs
->request
.dependencies
= realloc(cs
->request
.dependencies
, size
);
934 dep
= &cs
->request
.dependencies
[idx
];
935 memcpy(dep
, &bo_fence
->fence
, sizeof(*dep
));
938 for (unsigned j
= new_num_fences
; j
< bo
->num_fences
; ++j
)
939 amdgpu_fence_reference(&bo
->fences
[j
], NULL
);
941 bo
->num_fences
= new_num_fences
;
944 /* Since the kernel driver doesn't synchronize execution between different
945 * rings automatically, we have to add fence dependencies manually.
947 static void amdgpu_add_fence_dependencies(struct amdgpu_cs
*acs
)
949 struct amdgpu_cs_context
*cs
= acs
->csc
;
952 cs
->request
.number_of_dependencies
= 0;
954 for (i
= 0; i
< cs
->num_real_buffers
; i
++)
955 amdgpu_add_fence_dependency(acs
, &cs
->real_buffers
[i
]);
956 for (i
= 0; i
< cs
->num_slab_buffers
; i
++)
957 amdgpu_add_fence_dependency(acs
, &cs
->slab_buffers
[i
]);
960 static void amdgpu_add_fence(struct amdgpu_winsys_bo
*bo
,
961 struct pipe_fence_handle
*fence
)
963 if (bo
->num_fences
>= bo
->max_fences
) {
964 unsigned new_max_fences
= MAX2(1, bo
->max_fences
* 2);
965 struct pipe_fence_handle
**new_fences
=
967 bo
->num_fences
* sizeof(*new_fences
),
968 new_max_fences
* sizeof(*new_fences
));
970 bo
->fences
= new_fences
;
971 bo
->max_fences
= new_max_fences
;
973 fprintf(stderr
, "amdgpu_add_fence: allocation failure, dropping fence\n");
977 bo
->num_fences
--; /* prefer to keep a more recent fence if possible */
978 amdgpu_fence_reference(&bo
->fences
[bo
->num_fences
], NULL
);
982 bo
->fences
[bo
->num_fences
] = NULL
;
983 amdgpu_fence_reference(&bo
->fences
[bo
->num_fences
], fence
);
987 void amdgpu_cs_submit_ib(void *job
, int thread_index
)
989 struct amdgpu_cs
*acs
= (struct amdgpu_cs
*)job
;
990 struct amdgpu_winsys
*ws
= acs
->ctx
->ws
;
991 struct amdgpu_cs_context
*cs
= acs
->cst
;
994 cs
->request
.fence_info
.handle
= NULL
;
995 if (amdgpu_cs_has_user_fence(cs
)) {
996 cs
->request
.fence_info
.handle
= acs
->ctx
->user_fence_bo
;
997 cs
->request
.fence_info
.offset
= acs
->ring_type
;
1000 /* Create the buffer list.
1001 * Use a buffer list containing all allocated buffers if requested.
1003 if (debug_get_option_all_bos()) {
1004 struct amdgpu_winsys_bo
*bo
;
1005 amdgpu_bo_handle
*handles
;
1008 pipe_mutex_lock(ws
->global_bo_list_lock
);
1010 handles
= malloc(sizeof(handles
[0]) * ws
->num_buffers
);
1012 pipe_mutex_unlock(ws
->global_bo_list_lock
);
1013 amdgpu_cs_context_cleanup(cs
);
1014 cs
->error_code
= -ENOMEM
;
1018 LIST_FOR_EACH_ENTRY(bo
, &ws
->global_bo_list
, u
.real
.global_list_item
) {
1019 assert(num
< ws
->num_buffers
);
1020 handles
[num
++] = bo
->bo
;
1023 r
= amdgpu_bo_list_create(ws
->dev
, ws
->num_buffers
,
1025 &cs
->request
.resources
);
1027 pipe_mutex_unlock(ws
->global_bo_list_lock
);
1029 r
= amdgpu_bo_list_create(ws
->dev
, cs
->num_real_buffers
,
1030 cs
->handles
, cs
->flags
,
1031 &cs
->request
.resources
);
1035 fprintf(stderr
, "amdgpu: buffer list creation failed (%d)\n", r
);
1036 cs
->request
.resources
= NULL
;
1037 amdgpu_fence_signalled(cs
->fence
);
1042 if (acs
->ctx
->num_rejected_cs
)
1045 r
= amdgpu_cs_submit(acs
->ctx
->ctx
, 0, &cs
->request
, 1);
1050 fprintf(stderr
, "amdgpu: Not enough memory for command submission.\n");
1051 else if (r
== -ECANCELED
)
1052 fprintf(stderr
, "amdgpu: The CS has been cancelled because the context is lost.\n");
1054 fprintf(stderr
, "amdgpu: The CS has been rejected, "
1055 "see dmesg for more information (%i).\n", r
);
1057 amdgpu_fence_signalled(cs
->fence
);
1059 acs
->ctx
->num_rejected_cs
++;
1060 ws
->num_total_rejected_cs
++;
1063 uint64_t *user_fence
= NULL
;
1064 if (amdgpu_cs_has_user_fence(cs
))
1065 user_fence
= acs
->ctx
->user_fence_cpu_address_base
+
1066 cs
->request
.fence_info
.offset
;
1067 amdgpu_fence_submitted(cs
->fence
, &cs
->request
, user_fence
);
1071 if (cs
->request
.resources
)
1072 amdgpu_bo_list_destroy(cs
->request
.resources
);
1075 for (i
= 0; i
< cs
->num_real_buffers
; i
++)
1076 p_atomic_dec(&cs
->real_buffers
[i
].bo
->num_active_ioctls
);
1077 for (i
= 0; i
< cs
->num_slab_buffers
; i
++)
1078 p_atomic_dec(&cs
->slab_buffers
[i
].bo
->num_active_ioctls
);
1080 amdgpu_cs_context_cleanup(cs
);
1083 /* Make sure the previous submission is completed. */
1084 void amdgpu_cs_sync_flush(struct radeon_winsys_cs
*rcs
)
1086 struct amdgpu_cs
*cs
= amdgpu_cs(rcs
);
1088 /* Wait for any pending ioctl of this CS to complete. */
1089 util_queue_job_wait(&cs
->flush_completed
);
1092 static int amdgpu_cs_flush(struct radeon_winsys_cs
*rcs
,
1094 struct pipe_fence_handle
**fence
)
1096 struct amdgpu_cs
*cs
= amdgpu_cs(rcs
);
1097 struct amdgpu_winsys
*ws
= cs
->ctx
->ws
;
1100 rcs
->current
.max_dw
+= amdgpu_cs_epilog_dws(cs
->ring_type
);
1102 switch (cs
->ring_type
) {
1104 /* pad DMA ring to 8 DWs */
1105 if (ws
->info
.chip_class
<= SI
) {
1106 while (rcs
->current
.cdw
& 7)
1107 radeon_emit(rcs
, 0xf0000000); /* NOP packet */
1109 while (rcs
->current
.cdw
& 7)
1110 radeon_emit(rcs
, 0x00000000); /* NOP packet */
1114 /* pad GFX ring to 8 DWs to meet CP fetch alignment requirements */
1115 if (ws
->info
.gfx_ib_pad_with_type2
) {
1116 while (rcs
->current
.cdw
& 7)
1117 radeon_emit(rcs
, 0x80000000); /* type2 nop packet */
1119 while (rcs
->current
.cdw
& 7)
1120 radeon_emit(rcs
, 0xffff1000); /* type3 nop packet */
1123 /* Also pad the const IB. */
1124 if (cs
->const_ib
.ib_mapped
)
1125 while (!cs
->const_ib
.base
.current
.cdw
|| (cs
->const_ib
.base
.current
.cdw
& 7))
1126 radeon_emit(&cs
->const_ib
.base
, 0xffff1000); /* type3 nop packet */
1128 if (cs
->const_preamble_ib
.ib_mapped
)
1129 while (!cs
->const_preamble_ib
.base
.current
.cdw
|| (cs
->const_preamble_ib
.base
.current
.cdw
& 7))
1130 radeon_emit(&cs
->const_preamble_ib
.base
, 0xffff1000);
1133 while (rcs
->current
.cdw
& 15)
1134 radeon_emit(rcs
, 0x80000000); /* type2 nop packet */
1140 if (rcs
->current
.cdw
> rcs
->current
.max_dw
) {
1141 fprintf(stderr
, "amdgpu: command stream overflowed\n");
1144 /* If the CS is not empty or overflowed.... */
1145 if (radeon_emitted(&cs
->main
.base
, 0) &&
1146 cs
->main
.base
.current
.cdw
<= cs
->main
.base
.current
.max_dw
&&
1147 !debug_get_option_noop()) {
1148 struct amdgpu_cs_context
*cur
= cs
->csc
;
1149 unsigned i
, num_buffers
;
1152 amdgpu_ib_finalize(&cs
->main
);
1154 if (cs
->const_ib
.ib_mapped
)
1155 amdgpu_ib_finalize(&cs
->const_ib
);
1157 if (cs
->const_preamble_ib
.ib_mapped
)
1158 amdgpu_ib_finalize(&cs
->const_preamble_ib
);
1160 /* Create a fence. */
1161 amdgpu_fence_reference(&cur
->fence
, NULL
);
1162 if (cs
->next_fence
) {
1163 /* just move the reference */
1164 cur
->fence
= cs
->next_fence
;
1165 cs
->next_fence
= NULL
;
1167 cur
->fence
= amdgpu_fence_create(cs
->ctx
,
1168 cur
->request
.ip_type
,
1169 cur
->request
.ip_instance
,
1173 amdgpu_fence_reference(fence
, cur
->fence
);
1175 amdgpu_cs_sync_flush(rcs
);
1179 * This fence must be held until the submission is queued to ensure
1180 * that the order of fence dependency updates matches the order of
1183 pipe_mutex_lock(ws
->bo_fence_lock
);
1184 amdgpu_add_fence_dependencies(cs
);
1186 num_buffers
= cur
->num_real_buffers
;
1187 for (i
= 0; i
< num_buffers
; i
++) {
1188 struct amdgpu_winsys_bo
*bo
= cur
->real_buffers
[i
].bo
;
1189 p_atomic_inc(&bo
->num_active_ioctls
);
1190 amdgpu_add_fence(bo
, cur
->fence
);
1193 num_buffers
= cur
->num_slab_buffers
;
1194 for (i
= 0; i
< num_buffers
; i
++) {
1195 struct amdgpu_winsys_bo
*bo
= cur
->slab_buffers
[i
].bo
;
1196 p_atomic_inc(&bo
->num_active_ioctls
);
1197 amdgpu_add_fence(bo
, cur
->fence
);
1200 /* Swap command streams. "cst" is going to be submitted. */
1205 util_queue_add_job(&ws
->cs_queue
, cs
, &cs
->flush_completed
,
1206 amdgpu_cs_submit_ib
, NULL
);
1207 /* The submission has been queued, unlock the fence now. */
1208 pipe_mutex_unlock(ws
->bo_fence_lock
);
1210 if (!(flags
& RADEON_FLUSH_ASYNC
)) {
1211 amdgpu_cs_sync_flush(rcs
);
1212 error_code
= cur
->error_code
;
1215 amdgpu_cs_context_cleanup(cs
->csc
);
1218 amdgpu_get_new_ib(&ws
->base
, cs
, IB_MAIN
);
1219 if (cs
->const_ib
.ib_mapped
)
1220 amdgpu_get_new_ib(&ws
->base
, cs
, IB_CONST
);
1221 if (cs
->const_preamble_ib
.ib_mapped
)
1222 amdgpu_get_new_ib(&ws
->base
, cs
, IB_CONST_PREAMBLE
);
1224 cs
->main
.base
.used_gart
= 0;
1225 cs
->main
.base
.used_vram
= 0;
1227 if (cs
->ring_type
== RING_GFX
)
1229 else if (cs
->ring_type
== RING_DMA
)
1235 static void amdgpu_cs_destroy(struct radeon_winsys_cs
*rcs
)
1237 struct amdgpu_cs
*cs
= amdgpu_cs(rcs
);
1239 amdgpu_cs_sync_flush(rcs
);
1240 util_queue_fence_destroy(&cs
->flush_completed
);
1241 p_atomic_dec(&cs
->ctx
->ws
->num_cs
);
1242 pb_reference(&cs
->main
.big_ib_buffer
, NULL
);
1243 FREE(cs
->main
.base
.prev
);
1244 pb_reference(&cs
->const_ib
.big_ib_buffer
, NULL
);
1245 FREE(cs
->const_ib
.base
.prev
);
1246 pb_reference(&cs
->const_preamble_ib
.big_ib_buffer
, NULL
);
1247 FREE(cs
->const_preamble_ib
.base
.prev
);
1248 amdgpu_destroy_cs_context(&cs
->csc1
);
1249 amdgpu_destroy_cs_context(&cs
->csc2
);
1250 amdgpu_fence_reference(&cs
->next_fence
, NULL
);
1254 static bool amdgpu_bo_is_referenced(struct radeon_winsys_cs
*rcs
,
1255 struct pb_buffer
*_buf
,
1256 enum radeon_bo_usage usage
)
1258 struct amdgpu_cs
*cs
= amdgpu_cs(rcs
);
1259 struct amdgpu_winsys_bo
*bo
= (struct amdgpu_winsys_bo
*)_buf
;
1261 return amdgpu_bo_is_referenced_by_cs_with_usage(cs
, bo
, usage
);
1264 void amdgpu_cs_init_functions(struct amdgpu_winsys
*ws
)
1266 ws
->base
.ctx_create
= amdgpu_ctx_create
;
1267 ws
->base
.ctx_destroy
= amdgpu_ctx_destroy
;
1268 ws
->base
.ctx_query_reset_status
= amdgpu_ctx_query_reset_status
;
1269 ws
->base
.cs_create
= amdgpu_cs_create
;
1270 ws
->base
.cs_add_const_ib
= amdgpu_cs_add_const_ib
;
1271 ws
->base
.cs_add_const_preamble_ib
= amdgpu_cs_add_const_preamble_ib
;
1272 ws
->base
.cs_destroy
= amdgpu_cs_destroy
;
1273 ws
->base
.cs_add_buffer
= amdgpu_cs_add_buffer
;
1274 ws
->base
.cs_validate
= amdgpu_cs_validate
;
1275 ws
->base
.cs_check_space
= amdgpu_cs_check_space
;
1276 ws
->base
.cs_get_buffer_list
= amdgpu_cs_get_buffer_list
;
1277 ws
->base
.cs_flush
= amdgpu_cs_flush
;
1278 ws
->base
.cs_get_next_fence
= amdgpu_cs_get_next_fence
;
1279 ws
->base
.cs_is_buffer_referenced
= amdgpu_bo_is_referenced
;
1280 ws
->base
.cs_sync_flush
= amdgpu_cs_sync_flush
;
1281 ws
->base
.fence_wait
= amdgpu_fence_wait_rel_timeout
;
1282 ws
->base
.fence_reference
= amdgpu_fence_reference
;