amd: remove duplicated definitions from amdgpu_drm.h
[mesa.git] / src / gallium / winsys / amdgpu / drm / amdgpu_cs.c
1 /*
2 * Copyright © 2008 Jérôme Glisse
3 * Copyright © 2010 Marek Olšák <maraeo@gmail.com>
4 * Copyright © 2015 Advanced Micro Devices, Inc.
5 * All Rights Reserved.
6 *
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:
14 *
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.
23 *
24 * The above copyright notice and this permission notice (including the
25 * next paragraph) shall be included in all copies or substantial portions
26 * of the Software.
27 */
28
29 #include "amdgpu_cs.h"
30 #include "util/os_time.h"
31 #include <inttypes.h>
32 #include <stdio.h>
33
34 #include "amd/common/sid.h"
35
36 DEBUG_GET_ONCE_BOOL_OPTION(noop, "RADEON_NOOP", false)
37
38 /* FENCES */
39
40 static struct pipe_fence_handle *
41 amdgpu_fence_create(struct amdgpu_ctx *ctx, unsigned ip_type,
42 unsigned ip_instance, unsigned ring)
43 {
44 struct amdgpu_fence *fence = CALLOC_STRUCT(amdgpu_fence);
45
46 fence->reference.count = 1;
47 fence->ws = ctx->ws;
48 fence->ctx = ctx;
49 fence->fence.context = ctx->ctx;
50 fence->fence.ip_type = ip_type;
51 fence->fence.ip_instance = ip_instance;
52 fence->fence.ring = ring;
53 util_queue_fence_init(&fence->submitted);
54 util_queue_fence_reset(&fence->submitted);
55 p_atomic_inc(&ctx->refcount);
56 return (struct pipe_fence_handle *)fence;
57 }
58
59 static struct pipe_fence_handle *
60 amdgpu_fence_import_syncobj(struct radeon_winsys *rws, int fd)
61 {
62 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
63 struct amdgpu_fence *fence = CALLOC_STRUCT(amdgpu_fence);
64 int r;
65
66 if (!fence)
67 return NULL;
68
69 pipe_reference_init(&fence->reference, 1);
70 fence->ws = ws;
71
72 r = amdgpu_cs_import_syncobj(ws->dev, fd, &fence->syncobj);
73 if (r) {
74 FREE(fence);
75 return NULL;
76 }
77
78 util_queue_fence_init(&fence->submitted);
79
80 assert(amdgpu_fence_is_syncobj(fence));
81 return (struct pipe_fence_handle*)fence;
82 }
83
84 static struct pipe_fence_handle *
85 amdgpu_fence_import_sync_file(struct radeon_winsys *rws, int fd)
86 {
87 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
88 struct amdgpu_fence *fence = CALLOC_STRUCT(amdgpu_fence);
89
90 if (!fence)
91 return NULL;
92
93 pipe_reference_init(&fence->reference, 1);
94 fence->ws = ws;
95 /* fence->ctx == NULL means that the fence is syncobj-based. */
96
97 /* Convert sync_file into syncobj. */
98 int r = amdgpu_cs_create_syncobj(ws->dev, &fence->syncobj);
99 if (r) {
100 FREE(fence);
101 return NULL;
102 }
103
104 r = amdgpu_cs_syncobj_import_sync_file(ws->dev, fence->syncobj, fd);
105 if (r) {
106 amdgpu_cs_destroy_syncobj(ws->dev, fence->syncobj);
107 FREE(fence);
108 return NULL;
109 }
110
111 util_queue_fence_init(&fence->submitted);
112
113 return (struct pipe_fence_handle*)fence;
114 }
115
116 static int amdgpu_fence_export_sync_file(struct radeon_winsys *rws,
117 struct pipe_fence_handle *pfence)
118 {
119 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
120 struct amdgpu_fence *fence = (struct amdgpu_fence*)pfence;
121
122 if (amdgpu_fence_is_syncobj(fence)) {
123 int fd, r;
124
125 /* Convert syncobj into sync_file. */
126 r = amdgpu_cs_syncobj_export_sync_file(ws->dev, fence->syncobj, &fd);
127 return r ? -1 : fd;
128 }
129
130 util_queue_fence_wait(&fence->submitted);
131
132 /* Convert the amdgpu fence into a fence FD. */
133 int fd;
134 if (amdgpu_cs_fence_to_handle(ws->dev, &fence->fence,
135 AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD,
136 (uint32_t*)&fd))
137 return -1;
138
139 return fd;
140 }
141
142 static int amdgpu_export_signalled_sync_file(struct radeon_winsys *rws)
143 {
144 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
145 uint32_t syncobj;
146 int fd = -1;
147
148 int r = amdgpu_cs_create_syncobj2(ws->dev, DRM_SYNCOBJ_CREATE_SIGNALED,
149 &syncobj);
150 if (r) {
151 return -1;
152 }
153
154 r = amdgpu_cs_syncobj_export_sync_file(ws->dev, syncobj, &fd);
155 if (r) {
156 fd = -1;
157 }
158
159 amdgpu_cs_destroy_syncobj(ws->dev, syncobj);
160 return fd;
161 }
162
163 static void amdgpu_fence_submitted(struct pipe_fence_handle *fence,
164 uint64_t seq_no,
165 uint64_t *user_fence_cpu_address)
166 {
167 struct amdgpu_fence *afence = (struct amdgpu_fence*)fence;
168
169 afence->fence.fence = seq_no;
170 afence->user_fence_cpu_address = user_fence_cpu_address;
171 util_queue_fence_signal(&afence->submitted);
172 }
173
174 static void amdgpu_fence_signalled(struct pipe_fence_handle *fence)
175 {
176 struct amdgpu_fence *afence = (struct amdgpu_fence*)fence;
177
178 afence->signalled = true;
179 util_queue_fence_signal(&afence->submitted);
180 }
181
182 bool amdgpu_fence_wait(struct pipe_fence_handle *fence, uint64_t timeout,
183 bool absolute)
184 {
185 struct amdgpu_fence *afence = (struct amdgpu_fence*)fence;
186 uint32_t expired;
187 int64_t abs_timeout;
188 uint64_t *user_fence_cpu;
189 int r;
190
191 if (afence->signalled)
192 return true;
193
194 /* Handle syncobjs. */
195 if (amdgpu_fence_is_syncobj(afence)) {
196 /* Absolute timeouts are only be used by BO fences, which aren't
197 * backed by syncobjs.
198 */
199 assert(!absolute);
200
201 if (amdgpu_cs_syncobj_wait(afence->ws->dev, &afence->syncobj, 1,
202 timeout, 0, NULL))
203 return false;
204
205 afence->signalled = true;
206 return true;
207 }
208
209 if (absolute)
210 abs_timeout = timeout;
211 else
212 abs_timeout = os_time_get_absolute_timeout(timeout);
213
214 /* The fence might not have a number assigned if its IB is being
215 * submitted in the other thread right now. Wait until the submission
216 * is done. */
217 if (!util_queue_fence_wait_timeout(&afence->submitted, abs_timeout))
218 return false;
219
220 user_fence_cpu = afence->user_fence_cpu_address;
221 if (user_fence_cpu) {
222 if (*user_fence_cpu >= afence->fence.fence) {
223 afence->signalled = true;
224 return true;
225 }
226
227 /* No timeout, just query: no need for the ioctl. */
228 if (!absolute && !timeout)
229 return false;
230 }
231
232 /* Now use the libdrm query. */
233 r = amdgpu_cs_query_fence_status(&afence->fence,
234 abs_timeout,
235 AMDGPU_QUERY_FENCE_TIMEOUT_IS_ABSOLUTE,
236 &expired);
237 if (r) {
238 fprintf(stderr, "amdgpu: amdgpu_cs_query_fence_status failed.\n");
239 return false;
240 }
241
242 if (expired) {
243 /* This variable can only transition from false to true, so it doesn't
244 * matter if threads race for it. */
245 afence->signalled = true;
246 return true;
247 }
248 return false;
249 }
250
251 static bool amdgpu_fence_wait_rel_timeout(struct radeon_winsys *rws,
252 struct pipe_fence_handle *fence,
253 uint64_t timeout)
254 {
255 return amdgpu_fence_wait(fence, timeout, false);
256 }
257
258 static struct pipe_fence_handle *
259 amdgpu_cs_get_next_fence(struct radeon_cmdbuf *rcs)
260 {
261 struct amdgpu_cs *cs = amdgpu_cs(rcs);
262 struct pipe_fence_handle *fence = NULL;
263
264 if (debug_get_option_noop())
265 return NULL;
266
267 if (cs->next_fence) {
268 amdgpu_fence_reference(&fence, cs->next_fence);
269 return fence;
270 }
271
272 fence = amdgpu_fence_create(cs->ctx,
273 cs->csc->ib[IB_MAIN].ip_type,
274 cs->csc->ib[IB_MAIN].ip_instance,
275 cs->csc->ib[IB_MAIN].ring);
276 if (!fence)
277 return NULL;
278
279 amdgpu_fence_reference(&cs->next_fence, fence);
280 return fence;
281 }
282
283 /* CONTEXTS */
284
285 static struct radeon_winsys_ctx *amdgpu_ctx_create(struct radeon_winsys *ws)
286 {
287 struct amdgpu_ctx *ctx = CALLOC_STRUCT(amdgpu_ctx);
288 int r;
289 struct amdgpu_bo_alloc_request alloc_buffer = {};
290 amdgpu_bo_handle buf_handle;
291
292 if (!ctx)
293 return NULL;
294
295 ctx->ws = amdgpu_winsys(ws);
296 ctx->refcount = 1;
297 ctx->initial_num_total_rejected_cs = ctx->ws->num_total_rejected_cs;
298
299 r = amdgpu_cs_ctx_create(ctx->ws->dev, &ctx->ctx);
300 if (r) {
301 fprintf(stderr, "amdgpu: amdgpu_cs_ctx_create failed. (%i)\n", r);
302 goto error_create;
303 }
304
305 alloc_buffer.alloc_size = ctx->ws->info.gart_page_size;
306 alloc_buffer.phys_alignment = ctx->ws->info.gart_page_size;
307 alloc_buffer.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
308
309 r = amdgpu_bo_alloc(ctx->ws->dev, &alloc_buffer, &buf_handle);
310 if (r) {
311 fprintf(stderr, "amdgpu: amdgpu_bo_alloc failed. (%i)\n", r);
312 goto error_user_fence_alloc;
313 }
314
315 r = amdgpu_bo_cpu_map(buf_handle, (void**)&ctx->user_fence_cpu_address_base);
316 if (r) {
317 fprintf(stderr, "amdgpu: amdgpu_bo_cpu_map failed. (%i)\n", r);
318 goto error_user_fence_map;
319 }
320
321 memset(ctx->user_fence_cpu_address_base, 0, alloc_buffer.alloc_size);
322 ctx->user_fence_bo = buf_handle;
323
324 return (struct radeon_winsys_ctx*)ctx;
325
326 error_user_fence_map:
327 amdgpu_bo_free(buf_handle);
328 error_user_fence_alloc:
329 amdgpu_cs_ctx_free(ctx->ctx);
330 error_create:
331 FREE(ctx);
332 return NULL;
333 }
334
335 static void amdgpu_ctx_destroy(struct radeon_winsys_ctx *rwctx)
336 {
337 amdgpu_ctx_unref((struct amdgpu_ctx*)rwctx);
338 }
339
340 static enum pipe_reset_status
341 amdgpu_ctx_query_reset_status(struct radeon_winsys_ctx *rwctx)
342 {
343 struct amdgpu_ctx *ctx = (struct amdgpu_ctx*)rwctx;
344 int r;
345
346 /* Return a failure due to a GPU hang. */
347 if (ctx->ws->info.drm_minor >= 24) {
348 uint64_t flags;
349
350 r = amdgpu_cs_query_reset_state2(ctx->ctx, &flags);
351 if (r) {
352 fprintf(stderr, "amdgpu: amdgpu_cs_query_reset_state failed. (%i)\n", r);
353 return PIPE_NO_RESET;
354 }
355
356 if (flags & AMDGPU_CTX_QUERY2_FLAGS_RESET) {
357 if (flags & AMDGPU_CTX_QUERY2_FLAGS_GUILTY)
358 return PIPE_GUILTY_CONTEXT_RESET;
359 else
360 return PIPE_INNOCENT_CONTEXT_RESET;
361 }
362 } else {
363 uint32_t result, hangs;
364
365 r = amdgpu_cs_query_reset_state(ctx->ctx, &result, &hangs);
366 if (r) {
367 fprintf(stderr, "amdgpu: amdgpu_cs_query_reset_state failed. (%i)\n", r);
368 return PIPE_NO_RESET;
369 }
370
371 switch (result) {
372 case AMDGPU_CTX_GUILTY_RESET:
373 return PIPE_GUILTY_CONTEXT_RESET;
374 case AMDGPU_CTX_INNOCENT_RESET:
375 return PIPE_INNOCENT_CONTEXT_RESET;
376 case AMDGPU_CTX_UNKNOWN_RESET:
377 return PIPE_UNKNOWN_CONTEXT_RESET;
378 }
379 }
380
381 /* Return a failure due to a rejected command submission. */
382 if (ctx->ws->num_total_rejected_cs > ctx->initial_num_total_rejected_cs) {
383 return ctx->num_rejected_cs ? PIPE_GUILTY_CONTEXT_RESET :
384 PIPE_INNOCENT_CONTEXT_RESET;
385 }
386 return PIPE_NO_RESET;
387 }
388
389 /* COMMAND SUBMISSION */
390
391 static bool amdgpu_cs_has_user_fence(struct amdgpu_cs_context *cs)
392 {
393 return cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_UVD &&
394 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCE &&
395 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_UVD_ENC &&
396 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCN_DEC &&
397 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCN_ENC &&
398 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCN_JPEG;
399 }
400
401 static bool amdgpu_cs_has_chaining(struct amdgpu_cs *cs)
402 {
403 return cs->ctx->ws->info.chip_class >= GFX7 &&
404 (cs->ring_type == RING_GFX || cs->ring_type == RING_COMPUTE);
405 }
406
407 static unsigned amdgpu_cs_epilog_dws(struct amdgpu_cs *cs)
408 {
409 if (amdgpu_cs_has_chaining(cs))
410 return 4; /* for chaining */
411
412 return 0;
413 }
414
415 int amdgpu_lookup_buffer(struct amdgpu_cs_context *cs, struct amdgpu_winsys_bo *bo)
416 {
417 unsigned hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
418 int i = cs->buffer_indices_hashlist[hash];
419 struct amdgpu_cs_buffer *buffers;
420 int num_buffers;
421
422 if (bo->bo) {
423 buffers = cs->real_buffers;
424 num_buffers = cs->num_real_buffers;
425 } else if (!bo->sparse) {
426 buffers = cs->slab_buffers;
427 num_buffers = cs->num_slab_buffers;
428 } else {
429 buffers = cs->sparse_buffers;
430 num_buffers = cs->num_sparse_buffers;
431 }
432
433 /* not found or found */
434 if (i < 0 || (i < num_buffers && buffers[i].bo == bo))
435 return i;
436
437 /* Hash collision, look for the BO in the list of buffers linearly. */
438 for (i = num_buffers - 1; i >= 0; i--) {
439 if (buffers[i].bo == bo) {
440 /* Put this buffer in the hash list.
441 * This will prevent additional hash collisions if there are
442 * several consecutive lookup_buffer calls for the same buffer.
443 *
444 * Example: Assuming buffers A,B,C collide in the hash list,
445 * the following sequence of buffers:
446 * AAAAAAAAAAABBBBBBBBBBBBBBCCCCCCCC
447 * will collide here: ^ and here: ^,
448 * meaning that we should get very few collisions in the end. */
449 cs->buffer_indices_hashlist[hash] = i;
450 return i;
451 }
452 }
453 return -1;
454 }
455
456 static int
457 amdgpu_do_add_real_buffer(struct amdgpu_cs_context *cs, struct amdgpu_winsys_bo *bo)
458 {
459 struct amdgpu_cs_buffer *buffer;
460 int idx;
461
462 /* New buffer, check if the backing array is large enough. */
463 if (cs->num_real_buffers >= cs->max_real_buffers) {
464 unsigned new_max =
465 MAX2(cs->max_real_buffers + 16, (unsigned)(cs->max_real_buffers * 1.3));
466 struct amdgpu_cs_buffer *new_buffers;
467
468 new_buffers = MALLOC(new_max * sizeof(*new_buffers));
469
470 if (!new_buffers) {
471 fprintf(stderr, "amdgpu_do_add_buffer: allocation failed\n");
472 FREE(new_buffers);
473 return -1;
474 }
475
476 memcpy(new_buffers, cs->real_buffers, cs->num_real_buffers * sizeof(*new_buffers));
477
478 FREE(cs->real_buffers);
479
480 cs->max_real_buffers = new_max;
481 cs->real_buffers = new_buffers;
482 }
483
484 idx = cs->num_real_buffers;
485 buffer = &cs->real_buffers[idx];
486
487 memset(buffer, 0, sizeof(*buffer));
488 amdgpu_winsys_bo_reference(&buffer->bo, bo);
489 p_atomic_inc(&bo->num_cs_references);
490 cs->num_real_buffers++;
491
492 return idx;
493 }
494
495 static int
496 amdgpu_lookup_or_add_real_buffer(struct amdgpu_cs *acs, struct amdgpu_winsys_bo *bo)
497 {
498 struct amdgpu_cs_context *cs = acs->csc;
499 unsigned hash;
500 int idx = amdgpu_lookup_buffer(cs, bo);
501
502 if (idx >= 0)
503 return idx;
504
505 idx = amdgpu_do_add_real_buffer(cs, bo);
506
507 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
508 cs->buffer_indices_hashlist[hash] = idx;
509
510 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
511 acs->main.base.used_vram += bo->base.size;
512 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
513 acs->main.base.used_gart += bo->base.size;
514
515 return idx;
516 }
517
518 static int amdgpu_lookup_or_add_slab_buffer(struct amdgpu_cs *acs,
519 struct amdgpu_winsys_bo *bo)
520 {
521 struct amdgpu_cs_context *cs = acs->csc;
522 struct amdgpu_cs_buffer *buffer;
523 unsigned hash;
524 int idx = amdgpu_lookup_buffer(cs, bo);
525 int real_idx;
526
527 if (idx >= 0)
528 return idx;
529
530 real_idx = amdgpu_lookup_or_add_real_buffer(acs, bo->u.slab.real);
531 if (real_idx < 0)
532 return -1;
533
534 /* New buffer, check if the backing array is large enough. */
535 if (cs->num_slab_buffers >= cs->max_slab_buffers) {
536 unsigned new_max =
537 MAX2(cs->max_slab_buffers + 16, (unsigned)(cs->max_slab_buffers * 1.3));
538 struct amdgpu_cs_buffer *new_buffers;
539
540 new_buffers = REALLOC(cs->slab_buffers,
541 cs->max_slab_buffers * sizeof(*new_buffers),
542 new_max * sizeof(*new_buffers));
543 if (!new_buffers) {
544 fprintf(stderr, "amdgpu_lookup_or_add_slab_buffer: allocation failed\n");
545 return -1;
546 }
547
548 cs->max_slab_buffers = new_max;
549 cs->slab_buffers = new_buffers;
550 }
551
552 idx = cs->num_slab_buffers;
553 buffer = &cs->slab_buffers[idx];
554
555 memset(buffer, 0, sizeof(*buffer));
556 amdgpu_winsys_bo_reference(&buffer->bo, bo);
557 buffer->u.slab.real_idx = real_idx;
558 p_atomic_inc(&bo->num_cs_references);
559 cs->num_slab_buffers++;
560
561 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
562 cs->buffer_indices_hashlist[hash] = idx;
563
564 return idx;
565 }
566
567 static int amdgpu_lookup_or_add_sparse_buffer(struct amdgpu_cs *acs,
568 struct amdgpu_winsys_bo *bo)
569 {
570 struct amdgpu_cs_context *cs = acs->csc;
571 struct amdgpu_cs_buffer *buffer;
572 unsigned hash;
573 int idx = amdgpu_lookup_buffer(cs, bo);
574
575 if (idx >= 0)
576 return idx;
577
578 /* New buffer, check if the backing array is large enough. */
579 if (cs->num_sparse_buffers >= cs->max_sparse_buffers) {
580 unsigned new_max =
581 MAX2(cs->max_sparse_buffers + 16, (unsigned)(cs->max_sparse_buffers * 1.3));
582 struct amdgpu_cs_buffer *new_buffers;
583
584 new_buffers = REALLOC(cs->sparse_buffers,
585 cs->max_sparse_buffers * sizeof(*new_buffers),
586 new_max * sizeof(*new_buffers));
587 if (!new_buffers) {
588 fprintf(stderr, "amdgpu_lookup_or_add_sparse_buffer: allocation failed\n");
589 return -1;
590 }
591
592 cs->max_sparse_buffers = new_max;
593 cs->sparse_buffers = new_buffers;
594 }
595
596 idx = cs->num_sparse_buffers;
597 buffer = &cs->sparse_buffers[idx];
598
599 memset(buffer, 0, sizeof(*buffer));
600 amdgpu_winsys_bo_reference(&buffer->bo, bo);
601 p_atomic_inc(&bo->num_cs_references);
602 cs->num_sparse_buffers++;
603
604 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
605 cs->buffer_indices_hashlist[hash] = idx;
606
607 /* We delay adding the backing buffers until we really have to. However,
608 * we cannot delay accounting for memory use.
609 */
610 simple_mtx_lock(&bo->lock);
611
612 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
613 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
614 acs->main.base.used_vram += backing->bo->base.size;
615 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
616 acs->main.base.used_gart += backing->bo->base.size;
617 }
618
619 simple_mtx_unlock(&bo->lock);
620
621 return idx;
622 }
623
624 static unsigned amdgpu_cs_add_buffer(struct radeon_cmdbuf *rcs,
625 struct pb_buffer *buf,
626 enum radeon_bo_usage usage,
627 enum radeon_bo_domain domains,
628 enum radeon_bo_priority priority)
629 {
630 /* Don't use the "domains" parameter. Amdgpu doesn't support changing
631 * the buffer placement during command submission.
632 */
633 struct amdgpu_cs *acs = amdgpu_cs(rcs);
634 struct amdgpu_cs_context *cs = acs->csc;
635 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
636 struct amdgpu_cs_buffer *buffer;
637 int index;
638
639 /* Fast exit for no-op calls.
640 * This is very effective with suballocators and linear uploaders that
641 * are outside of the winsys.
642 */
643 if (bo == cs->last_added_bo &&
644 (usage & cs->last_added_bo_usage) == usage &&
645 (1u << priority) & cs->last_added_bo_priority_usage)
646 return cs->last_added_bo_index;
647
648 if (!bo->sparse) {
649 if (!bo->bo) {
650 index = amdgpu_lookup_or_add_slab_buffer(acs, bo);
651 if (index < 0)
652 return 0;
653
654 buffer = &cs->slab_buffers[index];
655 buffer->usage |= usage;
656
657 usage &= ~RADEON_USAGE_SYNCHRONIZED;
658 index = buffer->u.slab.real_idx;
659 } else {
660 index = amdgpu_lookup_or_add_real_buffer(acs, bo);
661 if (index < 0)
662 return 0;
663 }
664
665 buffer = &cs->real_buffers[index];
666 } else {
667 index = amdgpu_lookup_or_add_sparse_buffer(acs, bo);
668 if (index < 0)
669 return 0;
670
671 buffer = &cs->sparse_buffers[index];
672 }
673
674 buffer->u.real.priority_usage |= 1u << priority;
675 buffer->usage |= usage;
676
677 cs->last_added_bo = bo;
678 cs->last_added_bo_index = index;
679 cs->last_added_bo_usage = buffer->usage;
680 cs->last_added_bo_priority_usage = buffer->u.real.priority_usage;
681 return index;
682 }
683
684 static bool amdgpu_ib_new_buffer(struct amdgpu_winsys *ws,
685 struct amdgpu_ib *ib,
686 enum ring_type ring_type)
687 {
688 struct pb_buffer *pb;
689 uint8_t *mapped;
690 unsigned buffer_size;
691
692 /* Always create a buffer that is at least as large as the maximum seen IB
693 * size, aligned to a power of two (and multiplied by 4 to reduce internal
694 * fragmentation if chaining is not available). Limit to 512k dwords, which
695 * is the largest power of two that fits into the size field of the
696 * INDIRECT_BUFFER packet.
697 */
698 if (amdgpu_cs_has_chaining(amdgpu_cs_from_ib(ib)))
699 buffer_size = 4 *util_next_power_of_two(ib->max_ib_size);
700 else
701 buffer_size = 4 *util_next_power_of_two(4 * ib->max_ib_size);
702
703 const unsigned min_size = MAX2(ib->max_check_space_size, 8 * 1024 * 4);
704 const unsigned max_size = 512 * 1024 * 4;
705
706 buffer_size = MIN2(buffer_size, max_size);
707 buffer_size = MAX2(buffer_size, min_size); /* min_size is more important */
708
709 pb = amdgpu_bo_create(ws, buffer_size,
710 ws->info.gart_page_size,
711 RADEON_DOMAIN_GTT,
712 RADEON_FLAG_NO_INTERPROCESS_SHARING |
713 (ring_type == RING_GFX ||
714 ring_type == RING_COMPUTE ||
715 ring_type == RING_DMA ?
716 RADEON_FLAG_32BIT | RADEON_FLAG_GTT_WC : 0));
717 if (!pb)
718 return false;
719
720 mapped = amdgpu_bo_map(pb, NULL, PIPE_TRANSFER_WRITE);
721 if (!mapped) {
722 pb_reference(&pb, NULL);
723 return false;
724 }
725
726 pb_reference(&ib->big_ib_buffer, pb);
727 pb_reference(&pb, NULL);
728
729 ib->ib_mapped = mapped;
730 ib->used_ib_space = 0;
731
732 return true;
733 }
734
735 static unsigned amdgpu_ib_max_submit_dwords(enum ib_type ib_type)
736 {
737 /* The maximum IB size including all chained IBs. */
738 switch (ib_type) {
739 case IB_MAIN:
740 /* Smaller submits means the GPU gets busy sooner and there is less
741 * waiting for buffers and fences. Proof:
742 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
743 */
744 return 20 * 1024;
745 case IB_PARALLEL_COMPUTE:
746 /* Always chain this IB. */
747 return UINT_MAX;
748 default:
749 unreachable("bad ib_type");
750 }
751 }
752
753 static bool amdgpu_get_new_ib(struct amdgpu_winsys *ws, struct amdgpu_cs *cs,
754 enum ib_type ib_type)
755 {
756 /* Small IBs are better than big IBs, because the GPU goes idle quicker
757 * and there is less waiting for buffers and fences. Proof:
758 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
759 */
760 struct amdgpu_ib *ib = NULL;
761 struct drm_amdgpu_cs_chunk_ib *info = &cs->csc->ib[ib_type];
762 /* This is the minimum size of a contiguous IB. */
763 unsigned ib_size = 4 * 1024 * 4;
764
765 switch (ib_type) {
766 case IB_PARALLEL_COMPUTE:
767 ib = &cs->compute_ib;
768 break;
769 case IB_MAIN:
770 ib = &cs->main;
771 break;
772 default:
773 unreachable("unhandled IB type");
774 }
775
776 /* Always allocate at least the size of the biggest cs_check_space call,
777 * because precisely the last call might have requested this size.
778 */
779 ib_size = MAX2(ib_size, ib->max_check_space_size);
780
781 if (!amdgpu_cs_has_chaining(cs)) {
782 ib_size = MAX2(ib_size,
783 4 * MIN2(util_next_power_of_two(ib->max_ib_size),
784 amdgpu_ib_max_submit_dwords(ib_type)));
785 }
786
787 ib->max_ib_size = ib->max_ib_size - ib->max_ib_size / 32;
788
789 ib->base.prev_dw = 0;
790 ib->base.num_prev = 0;
791 ib->base.current.cdw = 0;
792 ib->base.current.buf = NULL;
793
794 /* Allocate a new buffer for IBs if the current buffer is all used. */
795 if (!ib->big_ib_buffer ||
796 ib->used_ib_space + ib_size > ib->big_ib_buffer->size) {
797 if (!amdgpu_ib_new_buffer(ws, ib, cs->ring_type))
798 return false;
799 }
800
801 info->va_start = amdgpu_winsys_bo(ib->big_ib_buffer)->va + ib->used_ib_space;
802 info->ib_bytes = 0;
803 /* ib_bytes is in dwords and the conversion to bytes will be done before
804 * the CS ioctl. */
805 ib->ptr_ib_size = &info->ib_bytes;
806 ib->ptr_ib_size_inside_ib = false;
807
808 amdgpu_cs_add_buffer(&cs->main.base, ib->big_ib_buffer,
809 RADEON_USAGE_READ, 0, RADEON_PRIO_IB1);
810
811 ib->base.current.buf = (uint32_t*)(ib->ib_mapped + ib->used_ib_space);
812
813 ib_size = ib->big_ib_buffer->size - ib->used_ib_space;
814 ib->base.current.max_dw = ib_size / 4 - amdgpu_cs_epilog_dws(cs);
815 assert(ib->base.current.max_dw >= ib->max_check_space_size / 4);
816 ib->base.gpu_address = info->va_start;
817 return true;
818 }
819
820 static void amdgpu_set_ib_size(struct amdgpu_ib *ib)
821 {
822 if (ib->ptr_ib_size_inside_ib) {
823 *ib->ptr_ib_size = ib->base.current.cdw |
824 S_3F2_CHAIN(1) | S_3F2_VALID(1);
825 } else {
826 *ib->ptr_ib_size = ib->base.current.cdw;
827 }
828 }
829
830 static void amdgpu_ib_finalize(struct amdgpu_winsys *ws, struct amdgpu_ib *ib)
831 {
832 amdgpu_set_ib_size(ib);
833 ib->used_ib_space += ib->base.current.cdw * 4;
834 ib->used_ib_space = align(ib->used_ib_space, ws->info.ib_start_alignment);
835 ib->max_ib_size = MAX2(ib->max_ib_size, ib->base.prev_dw + ib->base.current.cdw);
836 }
837
838 static bool amdgpu_init_cs_context(struct amdgpu_winsys *ws,
839 struct amdgpu_cs_context *cs,
840 enum ring_type ring_type)
841 {
842 switch (ring_type) {
843 case RING_DMA:
844 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_DMA;
845 break;
846
847 case RING_UVD:
848 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_UVD;
849 break;
850
851 case RING_UVD_ENC:
852 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_UVD_ENC;
853 break;
854
855 case RING_VCE:
856 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCE;
857 break;
858
859 case RING_VCN_DEC:
860 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCN_DEC;
861 break;
862
863 case RING_VCN_ENC:
864 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCN_ENC;
865 break;
866
867 case RING_VCN_JPEG:
868 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCN_JPEG;
869 break;
870
871 case RING_COMPUTE:
872 case RING_GFX:
873 cs->ib[IB_MAIN].ip_type = ring_type == RING_GFX ? AMDGPU_HW_IP_GFX :
874 AMDGPU_HW_IP_COMPUTE;
875
876 /* The kernel shouldn't invalidate L2 and vL1. The proper place for cache
877 * invalidation is the beginning of IBs (the previous commit does that),
878 * because completion of an IB doesn't care about the state of GPU caches,
879 * but the beginning of an IB does. Draw calls from multiple IBs can be
880 * executed in parallel, so draw calls from the current IB can finish after
881 * the next IB starts drawing, and so the cache flush at the end of IB
882 * is always late.
883 */
884 if (ws->info.drm_minor >= 26)
885 cs->ib[IB_MAIN].flags = AMDGPU_IB_FLAG_TC_WB_NOT_INVALIDATE;
886 break;
887
888 default:
889 assert(0);
890 }
891
892 cs->ib[IB_PARALLEL_COMPUTE].ip_type = AMDGPU_HW_IP_COMPUTE;
893 cs->ib[IB_PARALLEL_COMPUTE].flags = AMDGPU_IB_FLAG_TC_WB_NOT_INVALIDATE;
894
895 memset(cs->buffer_indices_hashlist, -1, sizeof(cs->buffer_indices_hashlist));
896 cs->last_added_bo = NULL;
897 return true;
898 }
899
900 static void cleanup_fence_list(struct amdgpu_fence_list *fences)
901 {
902 for (unsigned i = 0; i < fences->num; i++)
903 amdgpu_fence_reference(&fences->list[i], NULL);
904 fences->num = 0;
905 }
906
907 static void amdgpu_cs_context_cleanup(struct amdgpu_cs_context *cs)
908 {
909 unsigned i;
910
911 for (i = 0; i < cs->num_real_buffers; i++) {
912 p_atomic_dec(&cs->real_buffers[i].bo->num_cs_references);
913 amdgpu_winsys_bo_reference(&cs->real_buffers[i].bo, NULL);
914 }
915 for (i = 0; i < cs->num_slab_buffers; i++) {
916 p_atomic_dec(&cs->slab_buffers[i].bo->num_cs_references);
917 amdgpu_winsys_bo_reference(&cs->slab_buffers[i].bo, NULL);
918 }
919 for (i = 0; i < cs->num_sparse_buffers; i++) {
920 p_atomic_dec(&cs->sparse_buffers[i].bo->num_cs_references);
921 amdgpu_winsys_bo_reference(&cs->sparse_buffers[i].bo, NULL);
922 }
923 cleanup_fence_list(&cs->fence_dependencies);
924 cleanup_fence_list(&cs->syncobj_dependencies);
925 cleanup_fence_list(&cs->syncobj_to_signal);
926 cleanup_fence_list(&cs->compute_fence_dependencies);
927 cleanup_fence_list(&cs->compute_start_fence_dependencies);
928
929 cs->num_real_buffers = 0;
930 cs->num_slab_buffers = 0;
931 cs->num_sparse_buffers = 0;
932 amdgpu_fence_reference(&cs->fence, NULL);
933
934 memset(cs->buffer_indices_hashlist, -1, sizeof(cs->buffer_indices_hashlist));
935 cs->last_added_bo = NULL;
936 }
937
938 static void amdgpu_destroy_cs_context(struct amdgpu_cs_context *cs)
939 {
940 amdgpu_cs_context_cleanup(cs);
941 FREE(cs->real_buffers);
942 FREE(cs->slab_buffers);
943 FREE(cs->sparse_buffers);
944 FREE(cs->fence_dependencies.list);
945 FREE(cs->syncobj_dependencies.list);
946 FREE(cs->syncobj_to_signal.list);
947 FREE(cs->compute_fence_dependencies.list);
948 FREE(cs->compute_start_fence_dependencies.list);
949 }
950
951
952 static struct radeon_cmdbuf *
953 amdgpu_cs_create(struct radeon_winsys_ctx *rwctx,
954 enum ring_type ring_type,
955 void (*flush)(void *ctx, unsigned flags,
956 struct pipe_fence_handle **fence),
957 void *flush_ctx,
958 bool stop_exec_on_failure)
959 {
960 struct amdgpu_ctx *ctx = (struct amdgpu_ctx*)rwctx;
961 struct amdgpu_cs *cs;
962
963 cs = CALLOC_STRUCT(amdgpu_cs);
964 if (!cs) {
965 return NULL;
966 }
967
968 util_queue_fence_init(&cs->flush_completed);
969
970 cs->ctx = ctx;
971 cs->flush_cs = flush;
972 cs->flush_data = flush_ctx;
973 cs->ring_type = ring_type;
974 cs->stop_exec_on_failure = stop_exec_on_failure;
975
976 struct amdgpu_cs_fence_info fence_info;
977 fence_info.handle = cs->ctx->user_fence_bo;
978 fence_info.offset = cs->ring_type;
979 amdgpu_cs_chunk_fence_info_to_data(&fence_info, (void*)&cs->fence_chunk);
980
981 cs->main.ib_type = IB_MAIN;
982 cs->compute_ib.ib_type = IB_PARALLEL_COMPUTE;
983
984 if (!amdgpu_init_cs_context(ctx->ws, &cs->csc1, ring_type)) {
985 FREE(cs);
986 return NULL;
987 }
988
989 if (!amdgpu_init_cs_context(ctx->ws, &cs->csc2, ring_type)) {
990 amdgpu_destroy_cs_context(&cs->csc1);
991 FREE(cs);
992 return NULL;
993 }
994
995 /* Set the first submission context as current. */
996 cs->csc = &cs->csc1;
997 cs->cst = &cs->csc2;
998
999 if (!amdgpu_get_new_ib(ctx->ws, cs, IB_MAIN)) {
1000 amdgpu_destroy_cs_context(&cs->csc2);
1001 amdgpu_destroy_cs_context(&cs->csc1);
1002 FREE(cs);
1003 return NULL;
1004 }
1005
1006 p_atomic_inc(&ctx->ws->num_cs);
1007 return &cs->main.base;
1008 }
1009
1010 static struct radeon_cmdbuf *
1011 amdgpu_cs_add_parallel_compute_ib(struct radeon_cmdbuf *ib,
1012 bool uses_gds_ordered_append)
1013 {
1014 struct amdgpu_cs *cs = (struct amdgpu_cs*)ib;
1015 struct amdgpu_winsys *ws = cs->ctx->ws;
1016
1017 if (cs->ring_type != RING_GFX)
1018 return NULL;
1019
1020 /* only one secondary IB can be added */
1021 if (cs->compute_ib.ib_mapped)
1022 return NULL;
1023
1024 /* Allocate the compute IB. */
1025 if (!amdgpu_get_new_ib(ws, cs, IB_PARALLEL_COMPUTE))
1026 return NULL;
1027
1028 if (uses_gds_ordered_append) {
1029 cs->csc1.ib[IB_PARALLEL_COMPUTE].flags |=
1030 AMDGPU_IB_FLAG_RESET_GDS_MAX_WAVE_ID;
1031 cs->csc2.ib[IB_PARALLEL_COMPUTE].flags |=
1032 AMDGPU_IB_FLAG_RESET_GDS_MAX_WAVE_ID;
1033 }
1034 return &cs->compute_ib.base;
1035 }
1036
1037 static bool amdgpu_cs_validate(struct radeon_cmdbuf *rcs)
1038 {
1039 return true;
1040 }
1041
1042 static bool amdgpu_cs_check_space(struct radeon_cmdbuf *rcs, unsigned dw,
1043 bool force_chaining)
1044 {
1045 struct amdgpu_ib *ib = amdgpu_ib(rcs);
1046 struct amdgpu_cs *cs = amdgpu_cs_from_ib(ib);
1047 unsigned requested_size = rcs->prev_dw + rcs->current.cdw + dw;
1048 unsigned cs_epilog_dw = amdgpu_cs_epilog_dws(cs);
1049 unsigned need_byte_size = (dw + cs_epilog_dw) * 4;
1050 uint64_t va;
1051 uint32_t *new_ptr_ib_size;
1052
1053 assert(rcs->current.cdw <= rcs->current.max_dw);
1054
1055 /* 125% of the size for IB epilog. */
1056 unsigned safe_byte_size = need_byte_size + need_byte_size / 4;
1057 ib->max_check_space_size = MAX2(ib->max_check_space_size,
1058 safe_byte_size);
1059
1060 /* If force_chaining is true, we can't return. We have to chain. */
1061 if (!force_chaining) {
1062 if (requested_size > amdgpu_ib_max_submit_dwords(ib->ib_type))
1063 return false;
1064
1065 ib->max_ib_size = MAX2(ib->max_ib_size, requested_size);
1066
1067 if (rcs->current.max_dw - rcs->current.cdw >= dw)
1068 return true;
1069 }
1070
1071 if (!amdgpu_cs_has_chaining(cs)) {
1072 assert(!force_chaining);
1073 return false;
1074 }
1075
1076 /* Allocate a new chunk */
1077 if (rcs->num_prev >= rcs->max_prev) {
1078 unsigned new_max_prev = MAX2(1, 2 * rcs->max_prev);
1079 struct radeon_cmdbuf_chunk *new_prev;
1080
1081 new_prev = REALLOC(rcs->prev,
1082 sizeof(*new_prev) * rcs->max_prev,
1083 sizeof(*new_prev) * new_max_prev);
1084 if (!new_prev)
1085 return false;
1086
1087 rcs->prev = new_prev;
1088 rcs->max_prev = new_max_prev;
1089 }
1090
1091 if (!amdgpu_ib_new_buffer(cs->ctx->ws, ib, cs->ring_type))
1092 return false;
1093
1094 assert(ib->used_ib_space == 0);
1095 va = amdgpu_winsys_bo(ib->big_ib_buffer)->va;
1096
1097 /* This space was originally reserved. */
1098 rcs->current.max_dw += cs_epilog_dw;
1099
1100 /* Pad with NOPs and add INDIRECT_BUFFER packet */
1101 while ((rcs->current.cdw & 7) != 4)
1102 radeon_emit(rcs, 0xffff1000); /* type3 nop packet */
1103
1104 radeon_emit(rcs, PKT3(PKT3_INDIRECT_BUFFER_CIK, 2, 0));
1105 radeon_emit(rcs, va);
1106 radeon_emit(rcs, va >> 32);
1107 new_ptr_ib_size = &rcs->current.buf[rcs->current.cdw++];
1108
1109 assert((rcs->current.cdw & 7) == 0);
1110 assert(rcs->current.cdw <= rcs->current.max_dw);
1111
1112 amdgpu_set_ib_size(ib);
1113 ib->ptr_ib_size = new_ptr_ib_size;
1114 ib->ptr_ib_size_inside_ib = true;
1115
1116 /* Hook up the new chunk */
1117 rcs->prev[rcs->num_prev].buf = rcs->current.buf;
1118 rcs->prev[rcs->num_prev].cdw = rcs->current.cdw;
1119 rcs->prev[rcs->num_prev].max_dw = rcs->current.cdw; /* no modifications */
1120 rcs->num_prev++;
1121
1122 ib->base.prev_dw += ib->base.current.cdw;
1123 ib->base.current.cdw = 0;
1124
1125 ib->base.current.buf = (uint32_t*)(ib->ib_mapped + ib->used_ib_space);
1126 ib->base.current.max_dw = ib->big_ib_buffer->size / 4 - cs_epilog_dw;
1127 assert(ib->base.current.max_dw >= ib->max_check_space_size / 4);
1128 ib->base.gpu_address = va;
1129
1130 amdgpu_cs_add_buffer(&cs->main.base, ib->big_ib_buffer,
1131 RADEON_USAGE_READ, 0, RADEON_PRIO_IB1);
1132
1133 return true;
1134 }
1135
1136 static unsigned amdgpu_cs_get_buffer_list(struct radeon_cmdbuf *rcs,
1137 struct radeon_bo_list_item *list)
1138 {
1139 struct amdgpu_cs_context *cs = amdgpu_cs(rcs)->csc;
1140 int i;
1141
1142 if (list) {
1143 for (i = 0; i < cs->num_real_buffers; i++) {
1144 list[i].bo_size = cs->real_buffers[i].bo->base.size;
1145 list[i].vm_address = cs->real_buffers[i].bo->va;
1146 list[i].priority_usage = cs->real_buffers[i].u.real.priority_usage;
1147 }
1148 }
1149 return cs->num_real_buffers;
1150 }
1151
1152 static void add_fence_to_list(struct amdgpu_fence_list *fences,
1153 struct amdgpu_fence *fence)
1154 {
1155 unsigned idx = fences->num++;
1156
1157 if (idx >= fences->max) {
1158 unsigned size;
1159 const unsigned increment = 8;
1160
1161 fences->max = idx + increment;
1162 size = fences->max * sizeof(fences->list[0]);
1163 fences->list = realloc(fences->list, size);
1164 /* Clear the newly-allocated elements. */
1165 memset(fences->list + idx, 0,
1166 increment * sizeof(fences->list[0]));
1167 }
1168 amdgpu_fence_reference(&fences->list[idx], (struct pipe_fence_handle*)fence);
1169 }
1170
1171 static bool is_noop_fence_dependency(struct amdgpu_cs *acs,
1172 struct amdgpu_fence *fence)
1173 {
1174 struct amdgpu_cs_context *cs = acs->csc;
1175
1176 /* Detect no-op dependencies only when there is only 1 ring,
1177 * because IBs on one ring are always executed one at a time.
1178 *
1179 * We always want no dependency between back-to-back gfx IBs, because
1180 * we need the parallelism between IBs for good performance.
1181 */
1182 if ((acs->ring_type == RING_GFX ||
1183 acs->ctx->ws->info.num_rings[acs->ring_type] == 1) &&
1184 !amdgpu_fence_is_syncobj(fence) &&
1185 fence->ctx == acs->ctx &&
1186 fence->fence.ip_type == cs->ib[IB_MAIN].ip_type &&
1187 fence->fence.ip_instance == cs->ib[IB_MAIN].ip_instance &&
1188 fence->fence.ring == cs->ib[IB_MAIN].ring)
1189 return true;
1190
1191 return amdgpu_fence_wait((void *)fence, 0, false);
1192 }
1193
1194 static void amdgpu_cs_add_fence_dependency(struct radeon_cmdbuf *rws,
1195 struct pipe_fence_handle *pfence,
1196 unsigned dependency_flags)
1197 {
1198 struct amdgpu_cs *acs = amdgpu_cs(rws);
1199 struct amdgpu_cs_context *cs = acs->csc;
1200 struct amdgpu_fence *fence = (struct amdgpu_fence*)pfence;
1201
1202 util_queue_fence_wait(&fence->submitted);
1203
1204 if (dependency_flags & RADEON_DEPENDENCY_PARALLEL_COMPUTE_ONLY) {
1205 /* Syncobjs are not needed here. */
1206 assert(!amdgpu_fence_is_syncobj(fence));
1207
1208 if (acs->ctx->ws->info.has_scheduled_fence_dependency &&
1209 dependency_flags & RADEON_DEPENDENCY_START_FENCE)
1210 add_fence_to_list(&cs->compute_start_fence_dependencies, fence);
1211 else
1212 add_fence_to_list(&cs->compute_fence_dependencies, fence);
1213 return;
1214 }
1215
1216 /* Start fences are not needed here. */
1217 assert(!(dependency_flags & RADEON_DEPENDENCY_START_FENCE));
1218
1219 if (is_noop_fence_dependency(acs, fence))
1220 return;
1221
1222 if (amdgpu_fence_is_syncobj(fence))
1223 add_fence_to_list(&cs->syncobj_dependencies, fence);
1224 else
1225 add_fence_to_list(&cs->fence_dependencies, fence);
1226 }
1227
1228 static void amdgpu_add_bo_fence_dependencies(struct amdgpu_cs *acs,
1229 struct amdgpu_cs_buffer *buffer)
1230 {
1231 struct amdgpu_cs_context *cs = acs->csc;
1232 struct amdgpu_winsys_bo *bo = buffer->bo;
1233 unsigned new_num_fences = 0;
1234
1235 for (unsigned j = 0; j < bo->num_fences; ++j) {
1236 struct amdgpu_fence *bo_fence = (void *)bo->fences[j];
1237
1238 if (is_noop_fence_dependency(acs, bo_fence))
1239 continue;
1240
1241 amdgpu_fence_reference(&bo->fences[new_num_fences], bo->fences[j]);
1242 new_num_fences++;
1243
1244 if (!(buffer->usage & RADEON_USAGE_SYNCHRONIZED))
1245 continue;
1246
1247 add_fence_to_list(&cs->fence_dependencies, bo_fence);
1248 }
1249
1250 for (unsigned j = new_num_fences; j < bo->num_fences; ++j)
1251 amdgpu_fence_reference(&bo->fences[j], NULL);
1252
1253 bo->num_fences = new_num_fences;
1254 }
1255
1256 /* Add the given list of fences to the buffer's fence list.
1257 *
1258 * Must be called with the winsys bo_fence_lock held.
1259 */
1260 void amdgpu_add_fences(struct amdgpu_winsys_bo *bo,
1261 unsigned num_fences,
1262 struct pipe_fence_handle **fences)
1263 {
1264 if (bo->num_fences + num_fences > bo->max_fences) {
1265 unsigned new_max_fences = MAX2(bo->num_fences + num_fences, bo->max_fences * 2);
1266 struct pipe_fence_handle **new_fences =
1267 REALLOC(bo->fences,
1268 bo->num_fences * sizeof(*new_fences),
1269 new_max_fences * sizeof(*new_fences));
1270 if (likely(new_fences)) {
1271 bo->fences = new_fences;
1272 bo->max_fences = new_max_fences;
1273 } else {
1274 unsigned drop;
1275
1276 fprintf(stderr, "amdgpu_add_fences: allocation failure, dropping fence(s)\n");
1277 if (!bo->num_fences)
1278 return;
1279
1280 bo->num_fences--; /* prefer to keep the most recent fence if possible */
1281 amdgpu_fence_reference(&bo->fences[bo->num_fences], NULL);
1282
1283 drop = bo->num_fences + num_fences - bo->max_fences;
1284 num_fences -= drop;
1285 fences += drop;
1286 }
1287 }
1288
1289 for (unsigned i = 0; i < num_fences; ++i) {
1290 bo->fences[bo->num_fences] = NULL;
1291 amdgpu_fence_reference(&bo->fences[bo->num_fences], fences[i]);
1292 bo->num_fences++;
1293 }
1294 }
1295
1296 static void amdgpu_add_fence_dependencies_bo_list(struct amdgpu_cs *acs,
1297 struct pipe_fence_handle *fence,
1298 unsigned num_buffers,
1299 struct amdgpu_cs_buffer *buffers)
1300 {
1301 for (unsigned i = 0; i < num_buffers; i++) {
1302 struct amdgpu_cs_buffer *buffer = &buffers[i];
1303 struct amdgpu_winsys_bo *bo = buffer->bo;
1304
1305 amdgpu_add_bo_fence_dependencies(acs, buffer);
1306 p_atomic_inc(&bo->num_active_ioctls);
1307 amdgpu_add_fences(bo, 1, &fence);
1308 }
1309 }
1310
1311 /* Since the kernel driver doesn't synchronize execution between different
1312 * rings automatically, we have to add fence dependencies manually.
1313 */
1314 static void amdgpu_add_fence_dependencies_bo_lists(struct amdgpu_cs *acs)
1315 {
1316 struct amdgpu_cs_context *cs = acs->csc;
1317
1318 amdgpu_add_fence_dependencies_bo_list(acs, cs->fence, cs->num_real_buffers, cs->real_buffers);
1319 amdgpu_add_fence_dependencies_bo_list(acs, cs->fence, cs->num_slab_buffers, cs->slab_buffers);
1320 amdgpu_add_fence_dependencies_bo_list(acs, cs->fence, cs->num_sparse_buffers, cs->sparse_buffers);
1321 }
1322
1323 static void amdgpu_cs_add_syncobj_signal(struct radeon_cmdbuf *rws,
1324 struct pipe_fence_handle *fence)
1325 {
1326 struct amdgpu_cs *acs = amdgpu_cs(rws);
1327 struct amdgpu_cs_context *cs = acs->csc;
1328
1329 assert(amdgpu_fence_is_syncobj((struct amdgpu_fence *)fence));
1330
1331 add_fence_to_list(&cs->syncobj_to_signal, (struct amdgpu_fence*)fence);
1332 }
1333
1334 /* Add backing of sparse buffers to the buffer list.
1335 *
1336 * This is done late, during submission, to keep the buffer list short before
1337 * submit, and to avoid managing fences for the backing buffers.
1338 */
1339 static bool amdgpu_add_sparse_backing_buffers(struct amdgpu_cs_context *cs)
1340 {
1341 for (unsigned i = 0; i < cs->num_sparse_buffers; ++i) {
1342 struct amdgpu_cs_buffer *buffer = &cs->sparse_buffers[i];
1343 struct amdgpu_winsys_bo *bo = buffer->bo;
1344
1345 simple_mtx_lock(&bo->lock);
1346
1347 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
1348 /* We can directly add the buffer here, because we know that each
1349 * backing buffer occurs only once.
1350 */
1351 int idx = amdgpu_do_add_real_buffer(cs, backing->bo);
1352 if (idx < 0) {
1353 fprintf(stderr, "%s: failed to add buffer\n", __FUNCTION__);
1354 simple_mtx_unlock(&bo->lock);
1355 return false;
1356 }
1357
1358 cs->real_buffers[idx].usage = buffer->usage & ~RADEON_USAGE_SYNCHRONIZED;
1359 cs->real_buffers[idx].u.real.priority_usage = buffer->u.real.priority_usage;
1360 p_atomic_inc(&backing->bo->num_active_ioctls);
1361 }
1362
1363 simple_mtx_unlock(&bo->lock);
1364 }
1365
1366 return true;
1367 }
1368
1369 void amdgpu_cs_submit_ib(void *job, int thread_index)
1370 {
1371 struct amdgpu_cs *acs = (struct amdgpu_cs*)job;
1372 struct amdgpu_winsys *ws = acs->ctx->ws;
1373 struct amdgpu_cs_context *cs = acs->cst;
1374 int i, r;
1375 uint32_t bo_list = 0;
1376 uint64_t seq_no = 0;
1377 bool has_user_fence = amdgpu_cs_has_user_fence(cs);
1378 bool use_bo_list_create = ws->info.drm_minor < 27;
1379 struct drm_amdgpu_bo_list_in bo_list_in;
1380
1381 /* Prepare the buffer list. */
1382 if (ws->debug_all_bos) {
1383 /* The buffer list contains all buffers. This is a slow path that
1384 * ensures that no buffer is missing in the BO list.
1385 */
1386 unsigned num_handles = 0;
1387 struct drm_amdgpu_bo_list_entry *list =
1388 alloca(ws->num_buffers * sizeof(struct drm_amdgpu_bo_list_entry));
1389 struct amdgpu_winsys_bo *bo;
1390
1391 simple_mtx_lock(&ws->global_bo_list_lock);
1392 LIST_FOR_EACH_ENTRY(bo, &ws->global_bo_list, u.real.global_list_item) {
1393 list[num_handles].bo_handle = bo->u.real.kms_handle;
1394 list[num_handles].bo_priority = 0;
1395 ++num_handles;
1396 }
1397
1398 r = amdgpu_bo_list_create_raw(ws->dev, ws->num_buffers, list, &bo_list);
1399 simple_mtx_unlock(&ws->global_bo_list_lock);
1400 if (r) {
1401 fprintf(stderr, "amdgpu: buffer list creation failed (%d)\n", r);
1402 goto cleanup;
1403 }
1404 } else {
1405 if (!amdgpu_add_sparse_backing_buffers(cs)) {
1406 fprintf(stderr, "amdgpu: amdgpu_add_sparse_backing_buffers failed\n");
1407 r = -ENOMEM;
1408 goto cleanup;
1409 }
1410
1411 struct drm_amdgpu_bo_list_entry *list =
1412 alloca((cs->num_real_buffers + 2) * sizeof(struct drm_amdgpu_bo_list_entry));
1413
1414 unsigned num_handles = 0;
1415 for (i = 0; i < cs->num_real_buffers; ++i) {
1416 struct amdgpu_cs_buffer *buffer = &cs->real_buffers[i];
1417 assert(buffer->u.real.priority_usage != 0);
1418
1419 list[num_handles].bo_handle = buffer->bo->u.real.kms_handle;
1420 list[num_handles].bo_priority = (util_last_bit(buffer->u.real.priority_usage) - 1) / 2;
1421 ++num_handles;
1422 }
1423
1424 if (use_bo_list_create) {
1425 /* Legacy path creating the buffer list handle and passing it to the CS ioctl. */
1426 r = amdgpu_bo_list_create_raw(ws->dev, num_handles, list, &bo_list);
1427 if (r) {
1428 fprintf(stderr, "amdgpu: buffer list creation failed (%d)\n", r);
1429 goto cleanup;
1430 }
1431 } else {
1432 /* Standard path passing the buffer list via the CS ioctl. */
1433 bo_list_in.operation = ~0;
1434 bo_list_in.list_handle = ~0;
1435 bo_list_in.bo_number = num_handles;
1436 bo_list_in.bo_info_size = sizeof(struct drm_amdgpu_bo_list_entry);
1437 bo_list_in.bo_info_ptr = (uint64_t)(uintptr_t)list;
1438 }
1439 }
1440
1441 if (acs->ring_type == RING_GFX)
1442 ws->gfx_bo_list_counter += cs->num_real_buffers;
1443
1444 if (acs->stop_exec_on_failure && acs->ctx->num_rejected_cs) {
1445 r = -ECANCELED;
1446 } else {
1447 struct drm_amdgpu_cs_chunk chunks[6];
1448 unsigned num_chunks = 0;
1449
1450 /* BO list */
1451 if (!use_bo_list_create) {
1452 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_BO_HANDLES;
1453 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_bo_list_in) / 4;
1454 chunks[num_chunks].chunk_data = (uintptr_t)&bo_list_in;
1455 num_chunks++;
1456 }
1457
1458 /* Fence dependencies. */
1459 unsigned num_dependencies = cs->fence_dependencies.num;
1460 if (num_dependencies) {
1461 struct drm_amdgpu_cs_chunk_dep *dep_chunk =
1462 alloca(num_dependencies * sizeof(*dep_chunk));
1463
1464 for (unsigned i = 0; i < num_dependencies; i++) {
1465 struct amdgpu_fence *fence =
1466 (struct amdgpu_fence*)cs->fence_dependencies.list[i];
1467
1468 assert(util_queue_fence_is_signalled(&fence->submitted));
1469 amdgpu_cs_chunk_fence_to_dep(&fence->fence, &dep_chunk[i]);
1470 }
1471
1472 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_DEPENDENCIES;
1473 chunks[num_chunks].length_dw = sizeof(dep_chunk[0]) / 4 * num_dependencies;
1474 chunks[num_chunks].chunk_data = (uintptr_t)dep_chunk;
1475 num_chunks++;
1476 }
1477
1478 /* Syncobj dependencies. */
1479 unsigned num_syncobj_dependencies = cs->syncobj_dependencies.num;
1480 if (num_syncobj_dependencies) {
1481 struct drm_amdgpu_cs_chunk_sem *sem_chunk =
1482 alloca(num_syncobj_dependencies * sizeof(sem_chunk[0]));
1483
1484 for (unsigned i = 0; i < num_syncobj_dependencies; i++) {
1485 struct amdgpu_fence *fence =
1486 (struct amdgpu_fence*)cs->syncobj_dependencies.list[i];
1487
1488 if (!amdgpu_fence_is_syncobj(fence))
1489 continue;
1490
1491 assert(util_queue_fence_is_signalled(&fence->submitted));
1492 sem_chunk[i].handle = fence->syncobj;
1493 }
1494
1495 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_SYNCOBJ_IN;
1496 chunks[num_chunks].length_dw = sizeof(sem_chunk[0]) / 4 * num_syncobj_dependencies;
1497 chunks[num_chunks].chunk_data = (uintptr_t)sem_chunk;
1498 num_chunks++;
1499 }
1500
1501 /* Submit the parallel compute IB first. */
1502 if (cs->ib[IB_PARALLEL_COMPUTE].ib_bytes > 0) {
1503 unsigned old_num_chunks = num_chunks;
1504
1505 /* Add compute fence dependencies. */
1506 unsigned num_dependencies = cs->compute_fence_dependencies.num;
1507 if (num_dependencies) {
1508 struct drm_amdgpu_cs_chunk_dep *dep_chunk =
1509 alloca(num_dependencies * sizeof(*dep_chunk));
1510
1511 for (unsigned i = 0; i < num_dependencies; i++) {
1512 struct amdgpu_fence *fence =
1513 (struct amdgpu_fence*)cs->compute_fence_dependencies.list[i];
1514
1515 assert(util_queue_fence_is_signalled(&fence->submitted));
1516 amdgpu_cs_chunk_fence_to_dep(&fence->fence, &dep_chunk[i]);
1517 }
1518
1519 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_DEPENDENCIES;
1520 chunks[num_chunks].length_dw = sizeof(dep_chunk[0]) / 4 * num_dependencies;
1521 chunks[num_chunks].chunk_data = (uintptr_t)dep_chunk;
1522 num_chunks++;
1523 }
1524
1525 /* Add compute start fence dependencies. */
1526 unsigned num_start_dependencies = cs->compute_start_fence_dependencies.num;
1527 if (num_start_dependencies) {
1528 struct drm_amdgpu_cs_chunk_dep *dep_chunk =
1529 alloca(num_start_dependencies * sizeof(*dep_chunk));
1530
1531 for (unsigned i = 0; i < num_start_dependencies; i++) {
1532 struct amdgpu_fence *fence =
1533 (struct amdgpu_fence*)cs->compute_start_fence_dependencies.list[i];
1534
1535 assert(util_queue_fence_is_signalled(&fence->submitted));
1536 amdgpu_cs_chunk_fence_to_dep(&fence->fence, &dep_chunk[i]);
1537 }
1538
1539 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES;
1540 chunks[num_chunks].length_dw = sizeof(dep_chunk[0]) / 4 * num_start_dependencies;
1541 chunks[num_chunks].chunk_data = (uintptr_t)dep_chunk;
1542 num_chunks++;
1543 }
1544
1545 /* Convert from dwords to bytes. */
1546 cs->ib[IB_PARALLEL_COMPUTE].ib_bytes *= 4;
1547 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_IB;
1548 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_cs_chunk_ib) / 4;
1549 chunks[num_chunks].chunk_data = (uintptr_t)&cs->ib[IB_PARALLEL_COMPUTE];
1550 num_chunks++;
1551
1552 r = amdgpu_cs_submit_raw2(ws->dev, acs->ctx->ctx, bo_list,
1553 num_chunks, chunks, NULL);
1554 if (r)
1555 goto finalize;
1556
1557 /* Back off the compute chunks. */
1558 num_chunks = old_num_chunks;
1559 }
1560
1561 /* Syncobj signals. */
1562 unsigned num_syncobj_to_signal = cs->syncobj_to_signal.num;
1563 if (num_syncobj_to_signal) {
1564 struct drm_amdgpu_cs_chunk_sem *sem_chunk =
1565 alloca(num_syncobj_to_signal * sizeof(sem_chunk[0]));
1566
1567 for (unsigned i = 0; i < num_syncobj_to_signal; i++) {
1568 struct amdgpu_fence *fence =
1569 (struct amdgpu_fence*)cs->syncobj_to_signal.list[i];
1570
1571 assert(amdgpu_fence_is_syncobj(fence));
1572 sem_chunk[i].handle = fence->syncobj;
1573 }
1574
1575 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_SYNCOBJ_OUT;
1576 chunks[num_chunks].length_dw = sizeof(sem_chunk[0]) / 4
1577 * num_syncobj_to_signal;
1578 chunks[num_chunks].chunk_data = (uintptr_t)sem_chunk;
1579 num_chunks++;
1580 }
1581
1582 /* Fence */
1583 if (has_user_fence) {
1584 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_FENCE;
1585 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_cs_chunk_fence) / 4;
1586 chunks[num_chunks].chunk_data = (uintptr_t)&acs->fence_chunk;
1587 num_chunks++;
1588 }
1589
1590 /* IB */
1591 cs->ib[IB_MAIN].ib_bytes *= 4; /* Convert from dwords to bytes. */
1592 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_IB;
1593 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_cs_chunk_ib) / 4;
1594 chunks[num_chunks].chunk_data = (uintptr_t)&cs->ib[IB_MAIN];
1595 num_chunks++;
1596
1597 assert(num_chunks <= ARRAY_SIZE(chunks));
1598
1599 r = amdgpu_cs_submit_raw2(ws->dev, acs->ctx->ctx, bo_list,
1600 num_chunks, chunks, &seq_no);
1601 }
1602 finalize:
1603
1604 if (r) {
1605 if (r == -ENOMEM)
1606 fprintf(stderr, "amdgpu: Not enough memory for command submission.\n");
1607 else if (r == -ECANCELED)
1608 fprintf(stderr, "amdgpu: The CS has been cancelled because the context is lost.\n");
1609 else
1610 fprintf(stderr, "amdgpu: The CS has been rejected, "
1611 "see dmesg for more information (%i).\n", r);
1612
1613 acs->ctx->num_rejected_cs++;
1614 ws->num_total_rejected_cs++;
1615 } else {
1616 /* Success. */
1617 uint64_t *user_fence = NULL;
1618
1619 if (has_user_fence)
1620 user_fence = acs->ctx->user_fence_cpu_address_base + acs->ring_type;
1621 amdgpu_fence_submitted(cs->fence, seq_no, user_fence);
1622 }
1623
1624 /* Cleanup. */
1625 if (bo_list)
1626 amdgpu_bo_list_destroy_raw(ws->dev, bo_list);
1627
1628 cleanup:
1629 /* If there was an error, signal the fence, because it won't be signalled
1630 * by the hardware. */
1631 if (r)
1632 amdgpu_fence_signalled(cs->fence);
1633
1634 cs->error_code = r;
1635
1636 for (i = 0; i < cs->num_real_buffers; i++)
1637 p_atomic_dec(&cs->real_buffers[i].bo->num_active_ioctls);
1638 for (i = 0; i < cs->num_slab_buffers; i++)
1639 p_atomic_dec(&cs->slab_buffers[i].bo->num_active_ioctls);
1640 for (i = 0; i < cs->num_sparse_buffers; i++)
1641 p_atomic_dec(&cs->sparse_buffers[i].bo->num_active_ioctls);
1642
1643 amdgpu_cs_context_cleanup(cs);
1644 }
1645
1646 /* Make sure the previous submission is completed. */
1647 void amdgpu_cs_sync_flush(struct radeon_cmdbuf *rcs)
1648 {
1649 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1650
1651 /* Wait for any pending ioctl of this CS to complete. */
1652 util_queue_fence_wait(&cs->flush_completed);
1653 }
1654
1655 static int amdgpu_cs_flush(struct radeon_cmdbuf *rcs,
1656 unsigned flags,
1657 struct pipe_fence_handle **fence)
1658 {
1659 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1660 struct amdgpu_winsys *ws = cs->ctx->ws;
1661 int error_code = 0;
1662
1663 rcs->current.max_dw += amdgpu_cs_epilog_dws(cs);
1664
1665 switch (cs->ring_type) {
1666 case RING_DMA:
1667 /* pad DMA ring to 8 DWs */
1668 if (ws->info.chip_class <= GFX6) {
1669 while (rcs->current.cdw & 7)
1670 radeon_emit(rcs, 0xf0000000); /* NOP packet */
1671 }
1672 break;
1673 case RING_GFX:
1674 case RING_COMPUTE:
1675 /* pad GFX ring to 8 DWs to meet CP fetch alignment requirements */
1676 if (ws->info.gfx_ib_pad_with_type2) {
1677 while (rcs->current.cdw & 7)
1678 radeon_emit(rcs, 0x80000000); /* type2 nop packet */
1679 } else {
1680 while (rcs->current.cdw & 7)
1681 radeon_emit(rcs, 0xffff1000); /* type3 nop packet */
1682 }
1683 if (cs->ring_type == RING_GFX)
1684 ws->gfx_ib_size_counter += (rcs->prev_dw + rcs->current.cdw) * 4;
1685
1686 /* Also pad secondary IBs. */
1687 if (cs->compute_ib.ib_mapped) {
1688 while (cs->compute_ib.base.current.cdw & 7)
1689 radeon_emit(&cs->compute_ib.base, 0xffff1000); /* type3 nop packet */
1690 }
1691 break;
1692 case RING_UVD:
1693 case RING_UVD_ENC:
1694 while (rcs->current.cdw & 15)
1695 radeon_emit(rcs, 0x80000000); /* type2 nop packet */
1696 break;
1697 case RING_VCN_JPEG:
1698 if (rcs->current.cdw % 2)
1699 assert(0);
1700 while (rcs->current.cdw & 15) {
1701 radeon_emit(rcs, 0x60000000); /* nop packet */
1702 radeon_emit(rcs, 0x00000000);
1703 }
1704 break;
1705 case RING_VCN_DEC:
1706 while (rcs->current.cdw & 15)
1707 radeon_emit(rcs, 0x81ff); /* nop packet */
1708 break;
1709 default:
1710 break;
1711 }
1712
1713 if (rcs->current.cdw > rcs->current.max_dw) {
1714 fprintf(stderr, "amdgpu: command stream overflowed\n");
1715 }
1716
1717 /* If the CS is not empty or overflowed.... */
1718 if (likely(radeon_emitted(&cs->main.base, 0) &&
1719 cs->main.base.current.cdw <= cs->main.base.current.max_dw &&
1720 !debug_get_option_noop())) {
1721 struct amdgpu_cs_context *cur = cs->csc;
1722
1723 /* Set IB sizes. */
1724 amdgpu_ib_finalize(ws, &cs->main);
1725
1726 if (cs->compute_ib.ib_mapped)
1727 amdgpu_ib_finalize(ws, &cs->compute_ib);
1728
1729 /* Create a fence. */
1730 amdgpu_fence_reference(&cur->fence, NULL);
1731 if (cs->next_fence) {
1732 /* just move the reference */
1733 cur->fence = cs->next_fence;
1734 cs->next_fence = NULL;
1735 } else {
1736 cur->fence = amdgpu_fence_create(cs->ctx,
1737 cur->ib[IB_MAIN].ip_type,
1738 cur->ib[IB_MAIN].ip_instance,
1739 cur->ib[IB_MAIN].ring);
1740 }
1741 if (fence)
1742 amdgpu_fence_reference(fence, cur->fence);
1743
1744 amdgpu_cs_sync_flush(rcs);
1745
1746 /* Prepare buffers.
1747 *
1748 * This fence must be held until the submission is queued to ensure
1749 * that the order of fence dependency updates matches the order of
1750 * submissions.
1751 */
1752 simple_mtx_lock(&ws->bo_fence_lock);
1753 amdgpu_add_fence_dependencies_bo_lists(cs);
1754
1755 /* Swap command streams. "cst" is going to be submitted. */
1756 cs->csc = cs->cst;
1757 cs->cst = cur;
1758
1759 /* Submit. */
1760 util_queue_add_job(&ws->cs_queue, cs, &cs->flush_completed,
1761 amdgpu_cs_submit_ib, NULL, 0);
1762 /* The submission has been queued, unlock the fence now. */
1763 simple_mtx_unlock(&ws->bo_fence_lock);
1764
1765 if (!(flags & PIPE_FLUSH_ASYNC)) {
1766 amdgpu_cs_sync_flush(rcs);
1767 error_code = cur->error_code;
1768 }
1769 } else {
1770 amdgpu_cs_context_cleanup(cs->csc);
1771 }
1772
1773 amdgpu_get_new_ib(ws, cs, IB_MAIN);
1774 if (cs->compute_ib.ib_mapped)
1775 amdgpu_get_new_ib(ws, cs, IB_PARALLEL_COMPUTE);
1776
1777 cs->main.base.used_gart = 0;
1778 cs->main.base.used_vram = 0;
1779
1780 if (cs->ring_type == RING_GFX)
1781 ws->num_gfx_IBs++;
1782 else if (cs->ring_type == RING_DMA)
1783 ws->num_sdma_IBs++;
1784
1785 return error_code;
1786 }
1787
1788 static void amdgpu_cs_destroy(struct radeon_cmdbuf *rcs)
1789 {
1790 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1791
1792 amdgpu_cs_sync_flush(rcs);
1793 util_queue_fence_destroy(&cs->flush_completed);
1794 p_atomic_dec(&cs->ctx->ws->num_cs);
1795 pb_reference(&cs->main.big_ib_buffer, NULL);
1796 FREE(cs->main.base.prev);
1797 pb_reference(&cs->compute_ib.big_ib_buffer, NULL);
1798 FREE(cs->compute_ib.base.prev);
1799 amdgpu_destroy_cs_context(&cs->csc1);
1800 amdgpu_destroy_cs_context(&cs->csc2);
1801 amdgpu_fence_reference(&cs->next_fence, NULL);
1802 FREE(cs);
1803 }
1804
1805 static bool amdgpu_bo_is_referenced(struct radeon_cmdbuf *rcs,
1806 struct pb_buffer *_buf,
1807 enum radeon_bo_usage usage)
1808 {
1809 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1810 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)_buf;
1811
1812 return amdgpu_bo_is_referenced_by_cs_with_usage(cs, bo, usage);
1813 }
1814
1815 void amdgpu_cs_init_functions(struct amdgpu_screen_winsys *ws)
1816 {
1817 ws->base.ctx_create = amdgpu_ctx_create;
1818 ws->base.ctx_destroy = amdgpu_ctx_destroy;
1819 ws->base.ctx_query_reset_status = amdgpu_ctx_query_reset_status;
1820 ws->base.cs_create = amdgpu_cs_create;
1821 ws->base.cs_add_parallel_compute_ib = amdgpu_cs_add_parallel_compute_ib;
1822 ws->base.cs_destroy = amdgpu_cs_destroy;
1823 ws->base.cs_add_buffer = amdgpu_cs_add_buffer;
1824 ws->base.cs_validate = amdgpu_cs_validate;
1825 ws->base.cs_check_space = amdgpu_cs_check_space;
1826 ws->base.cs_get_buffer_list = amdgpu_cs_get_buffer_list;
1827 ws->base.cs_flush = amdgpu_cs_flush;
1828 ws->base.cs_get_next_fence = amdgpu_cs_get_next_fence;
1829 ws->base.cs_is_buffer_referenced = amdgpu_bo_is_referenced;
1830 ws->base.cs_sync_flush = amdgpu_cs_sync_flush;
1831 ws->base.cs_add_fence_dependency = amdgpu_cs_add_fence_dependency;
1832 ws->base.cs_add_syncobj_signal = amdgpu_cs_add_syncobj_signal;
1833 ws->base.fence_wait = amdgpu_fence_wait_rel_timeout;
1834 ws->base.fence_reference = amdgpu_fence_reference;
1835 ws->base.fence_import_syncobj = amdgpu_fence_import_syncobj;
1836 ws->base.fence_import_sync_file = amdgpu_fence_import_sync_file;
1837 ws->base.fence_export_sync_file = amdgpu_fence_export_sync_file;
1838 ws->base.export_signalled_sync_file = amdgpu_export_signalled_sync_file;
1839 }