winsys/amdgpu: add VCN JPEG to no user fence group
[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 uint32_t result, hangs;
345 int r;
346
347 /* Return a failure due to a rejected command submission. */
348 if (ctx->ws->num_total_rejected_cs > ctx->initial_num_total_rejected_cs) {
349 return ctx->num_rejected_cs ? PIPE_GUILTY_CONTEXT_RESET :
350 PIPE_INNOCENT_CONTEXT_RESET;
351 }
352
353 /* Return a failure due to a GPU hang. */
354 r = amdgpu_cs_query_reset_state(ctx->ctx, &result, &hangs);
355 if (r) {
356 fprintf(stderr, "amdgpu: amdgpu_cs_query_reset_state failed. (%i)\n", r);
357 return PIPE_NO_RESET;
358 }
359
360 switch (result) {
361 case AMDGPU_CTX_GUILTY_RESET:
362 return PIPE_GUILTY_CONTEXT_RESET;
363 case AMDGPU_CTX_INNOCENT_RESET:
364 return PIPE_INNOCENT_CONTEXT_RESET;
365 case AMDGPU_CTX_UNKNOWN_RESET:
366 return PIPE_UNKNOWN_CONTEXT_RESET;
367 case AMDGPU_CTX_NO_RESET:
368 default:
369 return PIPE_NO_RESET;
370 }
371 }
372
373 /* COMMAND SUBMISSION */
374
375 static bool amdgpu_cs_has_user_fence(struct amdgpu_cs_context *cs)
376 {
377 return cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_UVD &&
378 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCE &&
379 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_UVD_ENC &&
380 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCN_DEC &&
381 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCN_ENC &&
382 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCN_JPEG;
383 }
384
385 static bool amdgpu_cs_has_chaining(struct amdgpu_cs *cs)
386 {
387 return cs->ctx->ws->info.chip_class >= CIK &&
388 (cs->ring_type == RING_GFX || cs->ring_type == RING_COMPUTE);
389 }
390
391 static unsigned amdgpu_cs_epilog_dws(struct amdgpu_cs *cs)
392 {
393 if (amdgpu_cs_has_chaining(cs))
394 return 4; /* for chaining */
395
396 return 0;
397 }
398
399 int amdgpu_lookup_buffer(struct amdgpu_cs_context *cs, struct amdgpu_winsys_bo *bo)
400 {
401 unsigned hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
402 int i = cs->buffer_indices_hashlist[hash];
403 struct amdgpu_cs_buffer *buffers;
404 int num_buffers;
405
406 if (bo->bo) {
407 buffers = cs->real_buffers;
408 num_buffers = cs->num_real_buffers;
409 } else if (!bo->sparse) {
410 buffers = cs->slab_buffers;
411 num_buffers = cs->num_slab_buffers;
412 } else {
413 buffers = cs->sparse_buffers;
414 num_buffers = cs->num_sparse_buffers;
415 }
416
417 /* not found or found */
418 if (i < 0 || (i < num_buffers && buffers[i].bo == bo))
419 return i;
420
421 /* Hash collision, look for the BO in the list of buffers linearly. */
422 for (i = num_buffers - 1; i >= 0; i--) {
423 if (buffers[i].bo == bo) {
424 /* Put this buffer in the hash list.
425 * This will prevent additional hash collisions if there are
426 * several consecutive lookup_buffer calls for the same buffer.
427 *
428 * Example: Assuming buffers A,B,C collide in the hash list,
429 * the following sequence of buffers:
430 * AAAAAAAAAAABBBBBBBBBBBBBBCCCCCCCC
431 * will collide here: ^ and here: ^,
432 * meaning that we should get very few collisions in the end. */
433 cs->buffer_indices_hashlist[hash] = i;
434 return i;
435 }
436 }
437 return -1;
438 }
439
440 static int
441 amdgpu_do_add_real_buffer(struct amdgpu_cs_context *cs, struct amdgpu_winsys_bo *bo)
442 {
443 struct amdgpu_cs_buffer *buffer;
444 int idx;
445
446 /* New buffer, check if the backing array is large enough. */
447 if (cs->num_real_buffers >= cs->max_real_buffers) {
448 unsigned new_max =
449 MAX2(cs->max_real_buffers + 16, (unsigned)(cs->max_real_buffers * 1.3));
450 struct amdgpu_cs_buffer *new_buffers;
451
452 new_buffers = MALLOC(new_max * sizeof(*new_buffers));
453
454 if (!new_buffers) {
455 fprintf(stderr, "amdgpu_do_add_buffer: allocation failed\n");
456 FREE(new_buffers);
457 return -1;
458 }
459
460 memcpy(new_buffers, cs->real_buffers, cs->num_real_buffers * sizeof(*new_buffers));
461
462 FREE(cs->real_buffers);
463
464 cs->max_real_buffers = new_max;
465 cs->real_buffers = new_buffers;
466 }
467
468 idx = cs->num_real_buffers;
469 buffer = &cs->real_buffers[idx];
470
471 memset(buffer, 0, sizeof(*buffer));
472 amdgpu_winsys_bo_reference(&buffer->bo, bo);
473 p_atomic_inc(&bo->num_cs_references);
474 cs->num_real_buffers++;
475
476 return idx;
477 }
478
479 static int
480 amdgpu_lookup_or_add_real_buffer(struct amdgpu_cs *acs, struct amdgpu_winsys_bo *bo)
481 {
482 struct amdgpu_cs_context *cs = acs->csc;
483 unsigned hash;
484 int idx = amdgpu_lookup_buffer(cs, bo);
485
486 if (idx >= 0)
487 return idx;
488
489 idx = amdgpu_do_add_real_buffer(cs, bo);
490
491 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
492 cs->buffer_indices_hashlist[hash] = idx;
493
494 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
495 acs->main.base.used_vram += bo->base.size;
496 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
497 acs->main.base.used_gart += bo->base.size;
498
499 return idx;
500 }
501
502 static int amdgpu_lookup_or_add_slab_buffer(struct amdgpu_cs *acs,
503 struct amdgpu_winsys_bo *bo)
504 {
505 struct amdgpu_cs_context *cs = acs->csc;
506 struct amdgpu_cs_buffer *buffer;
507 unsigned hash;
508 int idx = amdgpu_lookup_buffer(cs, bo);
509 int real_idx;
510
511 if (idx >= 0)
512 return idx;
513
514 real_idx = amdgpu_lookup_or_add_real_buffer(acs, bo->u.slab.real);
515 if (real_idx < 0)
516 return -1;
517
518 /* New buffer, check if the backing array is large enough. */
519 if (cs->num_slab_buffers >= cs->max_slab_buffers) {
520 unsigned new_max =
521 MAX2(cs->max_slab_buffers + 16, (unsigned)(cs->max_slab_buffers * 1.3));
522 struct amdgpu_cs_buffer *new_buffers;
523
524 new_buffers = REALLOC(cs->slab_buffers,
525 cs->max_slab_buffers * sizeof(*new_buffers),
526 new_max * sizeof(*new_buffers));
527 if (!new_buffers) {
528 fprintf(stderr, "amdgpu_lookup_or_add_slab_buffer: allocation failed\n");
529 return -1;
530 }
531
532 cs->max_slab_buffers = new_max;
533 cs->slab_buffers = new_buffers;
534 }
535
536 idx = cs->num_slab_buffers;
537 buffer = &cs->slab_buffers[idx];
538
539 memset(buffer, 0, sizeof(*buffer));
540 amdgpu_winsys_bo_reference(&buffer->bo, bo);
541 buffer->u.slab.real_idx = real_idx;
542 p_atomic_inc(&bo->num_cs_references);
543 cs->num_slab_buffers++;
544
545 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
546 cs->buffer_indices_hashlist[hash] = idx;
547
548 return idx;
549 }
550
551 static int amdgpu_lookup_or_add_sparse_buffer(struct amdgpu_cs *acs,
552 struct amdgpu_winsys_bo *bo)
553 {
554 struct amdgpu_cs_context *cs = acs->csc;
555 struct amdgpu_cs_buffer *buffer;
556 unsigned hash;
557 int idx = amdgpu_lookup_buffer(cs, bo);
558
559 if (idx >= 0)
560 return idx;
561
562 /* New buffer, check if the backing array is large enough. */
563 if (cs->num_sparse_buffers >= cs->max_sparse_buffers) {
564 unsigned new_max =
565 MAX2(cs->max_sparse_buffers + 16, (unsigned)(cs->max_sparse_buffers * 1.3));
566 struct amdgpu_cs_buffer *new_buffers;
567
568 new_buffers = REALLOC(cs->sparse_buffers,
569 cs->max_sparse_buffers * sizeof(*new_buffers),
570 new_max * sizeof(*new_buffers));
571 if (!new_buffers) {
572 fprintf(stderr, "amdgpu_lookup_or_add_sparse_buffer: allocation failed\n");
573 return -1;
574 }
575
576 cs->max_sparse_buffers = new_max;
577 cs->sparse_buffers = new_buffers;
578 }
579
580 idx = cs->num_sparse_buffers;
581 buffer = &cs->sparse_buffers[idx];
582
583 memset(buffer, 0, sizeof(*buffer));
584 amdgpu_winsys_bo_reference(&buffer->bo, bo);
585 p_atomic_inc(&bo->num_cs_references);
586 cs->num_sparse_buffers++;
587
588 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
589 cs->buffer_indices_hashlist[hash] = idx;
590
591 /* We delay adding the backing buffers until we really have to. However,
592 * we cannot delay accounting for memory use.
593 */
594 simple_mtx_lock(&bo->lock);
595
596 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
597 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
598 acs->main.base.used_vram += backing->bo->base.size;
599 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
600 acs->main.base.used_gart += backing->bo->base.size;
601 }
602
603 simple_mtx_unlock(&bo->lock);
604
605 return idx;
606 }
607
608 static unsigned amdgpu_cs_add_buffer(struct radeon_cmdbuf *rcs,
609 struct pb_buffer *buf,
610 enum radeon_bo_usage usage,
611 enum radeon_bo_domain domains,
612 enum radeon_bo_priority priority)
613 {
614 /* Don't use the "domains" parameter. Amdgpu doesn't support changing
615 * the buffer placement during command submission.
616 */
617 struct amdgpu_cs *acs = amdgpu_cs(rcs);
618 struct amdgpu_cs_context *cs = acs->csc;
619 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
620 struct amdgpu_cs_buffer *buffer;
621 int index;
622
623 /* Fast exit for no-op calls.
624 * This is very effective with suballocators and linear uploaders that
625 * are outside of the winsys.
626 */
627 if (bo == cs->last_added_bo &&
628 (usage & cs->last_added_bo_usage) == usage &&
629 (1u << priority) & cs->last_added_bo_priority_usage)
630 return cs->last_added_bo_index;
631
632 if (!bo->sparse) {
633 if (!bo->bo) {
634 index = amdgpu_lookup_or_add_slab_buffer(acs, bo);
635 if (index < 0)
636 return 0;
637
638 buffer = &cs->slab_buffers[index];
639 buffer->usage |= usage;
640
641 usage &= ~RADEON_USAGE_SYNCHRONIZED;
642 index = buffer->u.slab.real_idx;
643 } else {
644 index = amdgpu_lookup_or_add_real_buffer(acs, bo);
645 if (index < 0)
646 return 0;
647 }
648
649 buffer = &cs->real_buffers[index];
650 } else {
651 index = amdgpu_lookup_or_add_sparse_buffer(acs, bo);
652 if (index < 0)
653 return 0;
654
655 buffer = &cs->sparse_buffers[index];
656 }
657
658 buffer->u.real.priority_usage |= 1u << priority;
659 buffer->usage |= usage;
660
661 cs->last_added_bo = bo;
662 cs->last_added_bo_index = index;
663 cs->last_added_bo_usage = buffer->usage;
664 cs->last_added_bo_priority_usage = buffer->u.real.priority_usage;
665 return index;
666 }
667
668 static bool amdgpu_ib_new_buffer(struct amdgpu_winsys *ws, struct amdgpu_ib *ib,
669 enum ring_type ring_type)
670 {
671 struct pb_buffer *pb;
672 uint8_t *mapped;
673 unsigned buffer_size;
674
675 /* Always create a buffer that is at least as large as the maximum seen IB
676 * size, aligned to a power of two (and multiplied by 4 to reduce internal
677 * fragmentation if chaining is not available). Limit to 512k dwords, which
678 * is the largest power of two that fits into the size field of the
679 * INDIRECT_BUFFER packet.
680 */
681 if (amdgpu_cs_has_chaining(amdgpu_cs_from_ib(ib)))
682 buffer_size = 4 *util_next_power_of_two(ib->max_ib_size);
683 else
684 buffer_size = 4 *util_next_power_of_two(4 * ib->max_ib_size);
685
686 const unsigned min_size = MAX2(ib->max_check_space_size, 8 * 1024 * 4);
687 const unsigned max_size = 512 * 1024 * 4;
688
689 buffer_size = MIN2(buffer_size, max_size);
690 buffer_size = MAX2(buffer_size, min_size); /* min_size is more important */
691
692 pb = ws->base.buffer_create(&ws->base, buffer_size,
693 ws->info.gart_page_size,
694 RADEON_DOMAIN_GTT,
695 RADEON_FLAG_NO_INTERPROCESS_SHARING |
696 (ring_type == RING_GFX ||
697 ring_type == RING_COMPUTE ||
698 ring_type == RING_DMA ?
699 RADEON_FLAG_32BIT | RADEON_FLAG_GTT_WC : 0));
700 if (!pb)
701 return false;
702
703 mapped = ws->base.buffer_map(pb, NULL, PIPE_TRANSFER_WRITE);
704 if (!mapped) {
705 pb_reference(&pb, NULL);
706 return false;
707 }
708
709 pb_reference(&ib->big_ib_buffer, pb);
710 pb_reference(&pb, NULL);
711
712 ib->ib_mapped = mapped;
713 ib->used_ib_space = 0;
714
715 return true;
716 }
717
718 static unsigned amdgpu_ib_max_submit_dwords(enum ib_type ib_type)
719 {
720 switch (ib_type) {
721 case IB_MAIN:
722 /* Smaller submits means the GPU gets busy sooner and there is less
723 * waiting for buffers and fences. Proof:
724 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
725 */
726 return 20 * 1024;
727 default:
728 unreachable("bad ib_type");
729 }
730 }
731
732 static bool amdgpu_get_new_ib(struct radeon_winsys *ws, struct amdgpu_cs *cs,
733 enum ib_type ib_type)
734 {
735 struct amdgpu_winsys *aws = (struct amdgpu_winsys*)ws;
736 /* Small IBs are better than big IBs, because the GPU goes idle quicker
737 * and there is less waiting for buffers and fences. Proof:
738 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
739 */
740 struct amdgpu_ib *ib = NULL;
741 struct drm_amdgpu_cs_chunk_ib *info = &cs->csc->ib[ib_type];
742 unsigned ib_size = 0;
743
744 switch (ib_type) {
745 case IB_MAIN:
746 ib = &cs->main;
747 ib_size = 4 * 1024 * 4;
748 break;
749 default:
750 unreachable("unhandled IB type");
751 }
752
753 /* Always allocate at least the size of the biggest cs_check_space call,
754 * because precisely the last call might have requested this size.
755 */
756 ib_size = MAX2(ib_size, ib->max_check_space_size);
757
758 if (!amdgpu_cs_has_chaining(cs)) {
759 ib_size = MAX2(ib_size,
760 4 * MIN2(util_next_power_of_two(ib->max_ib_size),
761 amdgpu_ib_max_submit_dwords(ib_type)));
762 }
763
764 ib->max_ib_size = ib->max_ib_size - ib->max_ib_size / 32;
765
766 ib->base.prev_dw = 0;
767 ib->base.num_prev = 0;
768 ib->base.current.cdw = 0;
769 ib->base.current.buf = NULL;
770
771 /* Allocate a new buffer for IBs if the current buffer is all used. */
772 if (!ib->big_ib_buffer ||
773 ib->used_ib_space + ib_size > ib->big_ib_buffer->size) {
774 if (!amdgpu_ib_new_buffer(aws, ib, cs->ring_type))
775 return false;
776 }
777
778 info->va_start = amdgpu_winsys_bo(ib->big_ib_buffer)->va + ib->used_ib_space;
779 info->ib_bytes = 0;
780 /* ib_bytes is in dwords and the conversion to bytes will be done before
781 * the CS ioctl. */
782 ib->ptr_ib_size = &info->ib_bytes;
783 ib->ptr_ib_size_inside_ib = false;
784
785 amdgpu_cs_add_buffer(&cs->main.base, ib->big_ib_buffer,
786 RADEON_USAGE_READ, 0, RADEON_PRIO_IB1);
787
788 ib->base.current.buf = (uint32_t*)(ib->ib_mapped + ib->used_ib_space);
789
790 ib_size = ib->big_ib_buffer->size - ib->used_ib_space;
791 ib->base.current.max_dw = ib_size / 4 - amdgpu_cs_epilog_dws(cs);
792 assert(ib->base.current.max_dw >= ib->max_check_space_size / 4);
793 ib->base.gpu_address = info->va_start;
794 return true;
795 }
796
797 static void amdgpu_set_ib_size(struct amdgpu_ib *ib)
798 {
799 if (ib->ptr_ib_size_inside_ib) {
800 *ib->ptr_ib_size = ib->base.current.cdw |
801 S_3F2_CHAIN(1) | S_3F2_VALID(1);
802 } else {
803 *ib->ptr_ib_size = ib->base.current.cdw;
804 }
805 }
806
807 static void amdgpu_ib_finalize(struct amdgpu_winsys *ws, struct amdgpu_ib *ib)
808 {
809 amdgpu_set_ib_size(ib);
810 ib->used_ib_space += ib->base.current.cdw * 4;
811 ib->used_ib_space = align(ib->used_ib_space, ws->info.ib_start_alignment);
812 ib->max_ib_size = MAX2(ib->max_ib_size, ib->base.prev_dw + ib->base.current.cdw);
813 }
814
815 static bool amdgpu_init_cs_context(struct amdgpu_winsys *ws,
816 struct amdgpu_cs_context *cs,
817 enum ring_type ring_type)
818 {
819 switch (ring_type) {
820 case RING_DMA:
821 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_DMA;
822 break;
823
824 case RING_UVD:
825 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_UVD;
826 break;
827
828 case RING_UVD_ENC:
829 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_UVD_ENC;
830 break;
831
832 case RING_VCE:
833 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCE;
834 break;
835
836 case RING_VCN_DEC:
837 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCN_DEC;
838 break;
839
840 case RING_VCN_ENC:
841 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCN_ENC;
842 break;
843
844 case RING_VCN_JPEG:
845 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCN_JPEG;
846 break;
847
848 case RING_COMPUTE:
849 case RING_GFX:
850 cs->ib[IB_MAIN].ip_type = ring_type == RING_GFX ? AMDGPU_HW_IP_GFX :
851 AMDGPU_HW_IP_COMPUTE;
852
853 /* The kernel shouldn't invalidate L2 and vL1. The proper place for cache
854 * invalidation is the beginning of IBs (the previous commit does that),
855 * because completion of an IB doesn't care about the state of GPU caches,
856 * but the beginning of an IB does. Draw calls from multiple IBs can be
857 * executed in parallel, so draw calls from the current IB can finish after
858 * the next IB starts drawing, and so the cache flush at the end of IB
859 * is always late.
860 */
861 if (ws->info.drm_minor >= 26)
862 cs->ib[IB_MAIN].flags = AMDGPU_IB_FLAG_TC_WB_NOT_INVALIDATE;
863 break;
864
865 default:
866 assert(0);
867 }
868
869 memset(cs->buffer_indices_hashlist, -1, sizeof(cs->buffer_indices_hashlist));
870 cs->last_added_bo = NULL;
871 return true;
872 }
873
874 static void cleanup_fence_list(struct amdgpu_fence_list *fences)
875 {
876 for (unsigned i = 0; i < fences->num; i++)
877 amdgpu_fence_reference(&fences->list[i], NULL);
878 fences->num = 0;
879 }
880
881 static void amdgpu_cs_context_cleanup(struct amdgpu_cs_context *cs)
882 {
883 unsigned i;
884
885 for (i = 0; i < cs->num_real_buffers; i++) {
886 p_atomic_dec(&cs->real_buffers[i].bo->num_cs_references);
887 amdgpu_winsys_bo_reference(&cs->real_buffers[i].bo, NULL);
888 }
889 for (i = 0; i < cs->num_slab_buffers; i++) {
890 p_atomic_dec(&cs->slab_buffers[i].bo->num_cs_references);
891 amdgpu_winsys_bo_reference(&cs->slab_buffers[i].bo, NULL);
892 }
893 for (i = 0; i < cs->num_sparse_buffers; i++) {
894 p_atomic_dec(&cs->sparse_buffers[i].bo->num_cs_references);
895 amdgpu_winsys_bo_reference(&cs->sparse_buffers[i].bo, NULL);
896 }
897 cleanup_fence_list(&cs->fence_dependencies);
898 cleanup_fence_list(&cs->syncobj_dependencies);
899 cleanup_fence_list(&cs->syncobj_to_signal);
900
901 cs->num_real_buffers = 0;
902 cs->num_slab_buffers = 0;
903 cs->num_sparse_buffers = 0;
904 amdgpu_fence_reference(&cs->fence, NULL);
905
906 memset(cs->buffer_indices_hashlist, -1, sizeof(cs->buffer_indices_hashlist));
907 cs->last_added_bo = NULL;
908 }
909
910 static void amdgpu_destroy_cs_context(struct amdgpu_cs_context *cs)
911 {
912 amdgpu_cs_context_cleanup(cs);
913 FREE(cs->real_buffers);
914 FREE(cs->slab_buffers);
915 FREE(cs->sparse_buffers);
916 FREE(cs->fence_dependencies.list);
917 FREE(cs->syncobj_dependencies.list);
918 FREE(cs->syncobj_to_signal.list);
919 }
920
921
922 static struct radeon_cmdbuf *
923 amdgpu_cs_create(struct radeon_winsys_ctx *rwctx,
924 enum ring_type ring_type,
925 void (*flush)(void *ctx, unsigned flags,
926 struct pipe_fence_handle **fence),
927 void *flush_ctx,
928 bool stop_exec_on_failure)
929 {
930 struct amdgpu_ctx *ctx = (struct amdgpu_ctx*)rwctx;
931 struct amdgpu_cs *cs;
932
933 cs = CALLOC_STRUCT(amdgpu_cs);
934 if (!cs) {
935 return NULL;
936 }
937
938 util_queue_fence_init(&cs->flush_completed);
939
940 cs->ctx = ctx;
941 cs->flush_cs = flush;
942 cs->flush_data = flush_ctx;
943 cs->ring_type = ring_type;
944 cs->stop_exec_on_failure = stop_exec_on_failure;
945
946 struct amdgpu_cs_fence_info fence_info;
947 fence_info.handle = cs->ctx->user_fence_bo;
948 fence_info.offset = cs->ring_type;
949 amdgpu_cs_chunk_fence_info_to_data(&fence_info, (void*)&cs->fence_chunk);
950
951 cs->main.ib_type = IB_MAIN;
952
953 if (!amdgpu_init_cs_context(ctx->ws, &cs->csc1, ring_type)) {
954 FREE(cs);
955 return NULL;
956 }
957
958 if (!amdgpu_init_cs_context(ctx->ws, &cs->csc2, ring_type)) {
959 amdgpu_destroy_cs_context(&cs->csc1);
960 FREE(cs);
961 return NULL;
962 }
963
964 /* Set the first submission context as current. */
965 cs->csc = &cs->csc1;
966 cs->cst = &cs->csc2;
967
968 if (!amdgpu_get_new_ib(&ctx->ws->base, cs, IB_MAIN)) {
969 amdgpu_destroy_cs_context(&cs->csc2);
970 amdgpu_destroy_cs_context(&cs->csc1);
971 FREE(cs);
972 return NULL;
973 }
974
975 p_atomic_inc(&ctx->ws->num_cs);
976 return &cs->main.base;
977 }
978
979 static bool amdgpu_cs_validate(struct radeon_cmdbuf *rcs)
980 {
981 return true;
982 }
983
984 static bool amdgpu_cs_check_space(struct radeon_cmdbuf *rcs, unsigned dw)
985 {
986 struct amdgpu_ib *ib = amdgpu_ib(rcs);
987 struct amdgpu_cs *cs = amdgpu_cs_from_ib(ib);
988 unsigned requested_size = rcs->prev_dw + rcs->current.cdw + dw;
989 unsigned cs_epilog_dw = amdgpu_cs_epilog_dws(cs);
990 unsigned need_byte_size = (dw + cs_epilog_dw) * 4;
991 uint64_t va;
992 uint32_t *new_ptr_ib_size;
993
994 assert(rcs->current.cdw <= rcs->current.max_dw);
995
996 /* 125% of the size for IB epilog. */
997 unsigned safe_byte_size = need_byte_size + need_byte_size / 4;
998 ib->max_check_space_size = MAX2(ib->max_check_space_size,
999 safe_byte_size);
1000
1001 if (requested_size > amdgpu_ib_max_submit_dwords(ib->ib_type))
1002 return false;
1003
1004 ib->max_ib_size = MAX2(ib->max_ib_size, requested_size);
1005
1006 if (rcs->current.max_dw - rcs->current.cdw >= dw)
1007 return true;
1008
1009 if (!amdgpu_cs_has_chaining(cs))
1010 return false;
1011
1012 /* Allocate a new chunk */
1013 if (rcs->num_prev >= rcs->max_prev) {
1014 unsigned new_max_prev = MAX2(1, 2 * rcs->max_prev);
1015 struct radeon_cmdbuf_chunk *new_prev;
1016
1017 new_prev = REALLOC(rcs->prev,
1018 sizeof(*new_prev) * rcs->max_prev,
1019 sizeof(*new_prev) * new_max_prev);
1020 if (!new_prev)
1021 return false;
1022
1023 rcs->prev = new_prev;
1024 rcs->max_prev = new_max_prev;
1025 }
1026
1027 if (!amdgpu_ib_new_buffer(cs->ctx->ws, ib, cs->ring_type))
1028 return false;
1029
1030 assert(ib->used_ib_space == 0);
1031 va = amdgpu_winsys_bo(ib->big_ib_buffer)->va;
1032
1033 /* This space was originally reserved. */
1034 rcs->current.max_dw += cs_epilog_dw;
1035
1036 /* Pad with NOPs and add INDIRECT_BUFFER packet */
1037 while ((rcs->current.cdw & 7) != 4)
1038 radeon_emit(rcs, 0xffff1000); /* type3 nop packet */
1039
1040 radeon_emit(rcs, PKT3(PKT3_INDIRECT_BUFFER_CIK, 2, 0));
1041 radeon_emit(rcs, va);
1042 radeon_emit(rcs, va >> 32);
1043 new_ptr_ib_size = &rcs->current.buf[rcs->current.cdw++];
1044
1045 assert((rcs->current.cdw & 7) == 0);
1046 assert(rcs->current.cdw <= rcs->current.max_dw);
1047
1048 amdgpu_set_ib_size(ib);
1049 ib->ptr_ib_size = new_ptr_ib_size;
1050 ib->ptr_ib_size_inside_ib = true;
1051
1052 /* Hook up the new chunk */
1053 rcs->prev[rcs->num_prev].buf = rcs->current.buf;
1054 rcs->prev[rcs->num_prev].cdw = rcs->current.cdw;
1055 rcs->prev[rcs->num_prev].max_dw = rcs->current.cdw; /* no modifications */
1056 rcs->num_prev++;
1057
1058 ib->base.prev_dw += ib->base.current.cdw;
1059 ib->base.current.cdw = 0;
1060
1061 ib->base.current.buf = (uint32_t*)(ib->ib_mapped + ib->used_ib_space);
1062 ib->base.current.max_dw = ib->big_ib_buffer->size / 4 - cs_epilog_dw;
1063 assert(ib->base.current.max_dw >= ib->max_check_space_size / 4);
1064 ib->base.gpu_address = va;
1065
1066 amdgpu_cs_add_buffer(&cs->main.base, ib->big_ib_buffer,
1067 RADEON_USAGE_READ, 0, RADEON_PRIO_IB1);
1068
1069 return true;
1070 }
1071
1072 static unsigned amdgpu_cs_get_buffer_list(struct radeon_cmdbuf *rcs,
1073 struct radeon_bo_list_item *list)
1074 {
1075 struct amdgpu_cs_context *cs = amdgpu_cs(rcs)->csc;
1076 int i;
1077
1078 if (list) {
1079 for (i = 0; i < cs->num_real_buffers; i++) {
1080 list[i].bo_size = cs->real_buffers[i].bo->base.size;
1081 list[i].vm_address = cs->real_buffers[i].bo->va;
1082 list[i].priority_usage = cs->real_buffers[i].u.real.priority_usage;
1083 }
1084 }
1085 return cs->num_real_buffers;
1086 }
1087
1088 static void add_fence_to_list(struct amdgpu_fence_list *fences,
1089 struct amdgpu_fence *fence)
1090 {
1091 unsigned idx = fences->num++;
1092
1093 if (idx >= fences->max) {
1094 unsigned size;
1095 const unsigned increment = 8;
1096
1097 fences->max = idx + increment;
1098 size = fences->max * sizeof(fences->list[0]);
1099 fences->list = realloc(fences->list, size);
1100 /* Clear the newly-allocated elements. */
1101 memset(fences->list + idx, 0,
1102 increment * sizeof(fences->list[0]));
1103 }
1104 amdgpu_fence_reference(&fences->list[idx], (struct pipe_fence_handle*)fence);
1105 }
1106
1107 static bool is_noop_fence_dependency(struct amdgpu_cs *acs,
1108 struct amdgpu_fence *fence)
1109 {
1110 struct amdgpu_cs_context *cs = acs->csc;
1111
1112 if (!amdgpu_fence_is_syncobj(fence) &&
1113 fence->ctx == acs->ctx &&
1114 fence->fence.ip_type == cs->ib[IB_MAIN].ip_type &&
1115 fence->fence.ip_instance == cs->ib[IB_MAIN].ip_instance &&
1116 fence->fence.ring == cs->ib[IB_MAIN].ring)
1117 return true;
1118
1119 return amdgpu_fence_wait((void *)fence, 0, false);
1120 }
1121
1122 static void amdgpu_cs_add_fence_dependency(struct radeon_cmdbuf *rws,
1123 struct pipe_fence_handle *pfence)
1124 {
1125 struct amdgpu_cs *acs = amdgpu_cs(rws);
1126 struct amdgpu_cs_context *cs = acs->csc;
1127 struct amdgpu_fence *fence = (struct amdgpu_fence*)pfence;
1128
1129 util_queue_fence_wait(&fence->submitted);
1130
1131 if (is_noop_fence_dependency(acs, fence))
1132 return;
1133
1134 if (amdgpu_fence_is_syncobj(fence))
1135 add_fence_to_list(&cs->syncobj_dependencies, fence);
1136 else
1137 add_fence_to_list(&cs->fence_dependencies, fence);
1138 }
1139
1140 static void amdgpu_add_bo_fence_dependencies(struct amdgpu_cs *acs,
1141 struct amdgpu_cs_buffer *buffer)
1142 {
1143 struct amdgpu_cs_context *cs = acs->csc;
1144 struct amdgpu_winsys_bo *bo = buffer->bo;
1145 unsigned new_num_fences = 0;
1146
1147 for (unsigned j = 0; j < bo->num_fences; ++j) {
1148 struct amdgpu_fence *bo_fence = (void *)bo->fences[j];
1149
1150 if (is_noop_fence_dependency(acs, bo_fence))
1151 continue;
1152
1153 amdgpu_fence_reference(&bo->fences[new_num_fences], bo->fences[j]);
1154 new_num_fences++;
1155
1156 if (!(buffer->usage & RADEON_USAGE_SYNCHRONIZED))
1157 continue;
1158
1159 add_fence_to_list(&cs->fence_dependencies, bo_fence);
1160 }
1161
1162 for (unsigned j = new_num_fences; j < bo->num_fences; ++j)
1163 amdgpu_fence_reference(&bo->fences[j], NULL);
1164
1165 bo->num_fences = new_num_fences;
1166 }
1167
1168 /* Add the given list of fences to the buffer's fence list.
1169 *
1170 * Must be called with the winsys bo_fence_lock held.
1171 */
1172 void amdgpu_add_fences(struct amdgpu_winsys_bo *bo,
1173 unsigned num_fences,
1174 struct pipe_fence_handle **fences)
1175 {
1176 if (bo->num_fences + num_fences > bo->max_fences) {
1177 unsigned new_max_fences = MAX2(bo->num_fences + num_fences, bo->max_fences * 2);
1178 struct pipe_fence_handle **new_fences =
1179 REALLOC(bo->fences,
1180 bo->num_fences * sizeof(*new_fences),
1181 new_max_fences * sizeof(*new_fences));
1182 if (likely(new_fences)) {
1183 bo->fences = new_fences;
1184 bo->max_fences = new_max_fences;
1185 } else {
1186 unsigned drop;
1187
1188 fprintf(stderr, "amdgpu_add_fences: allocation failure, dropping fence(s)\n");
1189 if (!bo->num_fences)
1190 return;
1191
1192 bo->num_fences--; /* prefer to keep the most recent fence if possible */
1193 amdgpu_fence_reference(&bo->fences[bo->num_fences], NULL);
1194
1195 drop = bo->num_fences + num_fences - bo->max_fences;
1196 num_fences -= drop;
1197 fences += drop;
1198 }
1199 }
1200
1201 for (unsigned i = 0; i < num_fences; ++i) {
1202 bo->fences[bo->num_fences] = NULL;
1203 amdgpu_fence_reference(&bo->fences[bo->num_fences], fences[i]);
1204 bo->num_fences++;
1205 }
1206 }
1207
1208 static void amdgpu_add_fence_dependencies_bo_list(struct amdgpu_cs *acs,
1209 struct pipe_fence_handle *fence,
1210 unsigned num_buffers,
1211 struct amdgpu_cs_buffer *buffers)
1212 {
1213 for (unsigned i = 0; i < num_buffers; i++) {
1214 struct amdgpu_cs_buffer *buffer = &buffers[i];
1215 struct amdgpu_winsys_bo *bo = buffer->bo;
1216
1217 amdgpu_add_bo_fence_dependencies(acs, buffer);
1218 p_atomic_inc(&bo->num_active_ioctls);
1219 amdgpu_add_fences(bo, 1, &fence);
1220 }
1221 }
1222
1223 /* Since the kernel driver doesn't synchronize execution between different
1224 * rings automatically, we have to add fence dependencies manually.
1225 */
1226 static void amdgpu_add_fence_dependencies_bo_lists(struct amdgpu_cs *acs)
1227 {
1228 struct amdgpu_cs_context *cs = acs->csc;
1229
1230 amdgpu_add_fence_dependencies_bo_list(acs, cs->fence, cs->num_real_buffers, cs->real_buffers);
1231 amdgpu_add_fence_dependencies_bo_list(acs, cs->fence, cs->num_slab_buffers, cs->slab_buffers);
1232 amdgpu_add_fence_dependencies_bo_list(acs, cs->fence, cs->num_sparse_buffers, cs->sparse_buffers);
1233 }
1234
1235 static void amdgpu_cs_add_syncobj_signal(struct radeon_cmdbuf *rws,
1236 struct pipe_fence_handle *fence)
1237 {
1238 struct amdgpu_cs *acs = amdgpu_cs(rws);
1239 struct amdgpu_cs_context *cs = acs->csc;
1240
1241 assert(amdgpu_fence_is_syncobj((struct amdgpu_fence *)fence));
1242
1243 add_fence_to_list(&cs->syncobj_to_signal, (struct amdgpu_fence*)fence);
1244 }
1245
1246 /* Add backing of sparse buffers to the buffer list.
1247 *
1248 * This is done late, during submission, to keep the buffer list short before
1249 * submit, and to avoid managing fences for the backing buffers.
1250 */
1251 static bool amdgpu_add_sparse_backing_buffers(struct amdgpu_cs_context *cs)
1252 {
1253 for (unsigned i = 0; i < cs->num_sparse_buffers; ++i) {
1254 struct amdgpu_cs_buffer *buffer = &cs->sparse_buffers[i];
1255 struct amdgpu_winsys_bo *bo = buffer->bo;
1256
1257 simple_mtx_lock(&bo->lock);
1258
1259 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
1260 /* We can directly add the buffer here, because we know that each
1261 * backing buffer occurs only once.
1262 */
1263 int idx = amdgpu_do_add_real_buffer(cs, backing->bo);
1264 if (idx < 0) {
1265 fprintf(stderr, "%s: failed to add buffer\n", __FUNCTION__);
1266 simple_mtx_unlock(&bo->lock);
1267 return false;
1268 }
1269
1270 cs->real_buffers[idx].usage = buffer->usage & ~RADEON_USAGE_SYNCHRONIZED;
1271 cs->real_buffers[idx].u.real.priority_usage = buffer->u.real.priority_usage;
1272 p_atomic_inc(&backing->bo->num_active_ioctls);
1273 }
1274
1275 simple_mtx_unlock(&bo->lock);
1276 }
1277
1278 return true;
1279 }
1280
1281 void amdgpu_cs_submit_ib(void *job, int thread_index)
1282 {
1283 struct amdgpu_cs *acs = (struct amdgpu_cs*)job;
1284 struct amdgpu_winsys *ws = acs->ctx->ws;
1285 struct amdgpu_cs_context *cs = acs->cst;
1286 int i, r;
1287 uint32_t bo_list = 0;
1288 uint64_t seq_no = 0;
1289 bool has_user_fence = amdgpu_cs_has_user_fence(cs);
1290 bool use_bo_list_create = ws->info.drm_minor < 27;
1291 struct drm_amdgpu_bo_list_in bo_list_in;
1292
1293 /* Prepare the buffer list. */
1294 if (ws->debug_all_bos) {
1295 /* The buffer list contains all buffers. This is a slow path that
1296 * ensures that no buffer is missing in the BO list.
1297 */
1298 unsigned num_handles = 0;
1299 struct drm_amdgpu_bo_list_entry *list =
1300 alloca(ws->num_buffers * sizeof(struct drm_amdgpu_bo_list_entry));
1301 struct amdgpu_winsys_bo *bo;
1302
1303 simple_mtx_lock(&ws->global_bo_list_lock);
1304 LIST_FOR_EACH_ENTRY(bo, &ws->global_bo_list, u.real.global_list_item) {
1305 if (bo->is_local)
1306 continue;
1307
1308 list[num_handles].bo_handle = bo->u.real.kms_handle;
1309 list[num_handles].bo_priority = 0;
1310 ++num_handles;
1311 }
1312
1313 r = amdgpu_bo_list_create_raw(ws->dev, ws->num_buffers, list, &bo_list);
1314 simple_mtx_unlock(&ws->global_bo_list_lock);
1315 if (r) {
1316 fprintf(stderr, "amdgpu: buffer list creation failed (%d)\n", r);
1317 goto cleanup;
1318 }
1319 } else {
1320 if (!amdgpu_add_sparse_backing_buffers(cs)) {
1321 fprintf(stderr, "amdgpu: amdgpu_add_sparse_backing_buffers failed\n");
1322 r = -ENOMEM;
1323 goto cleanup;
1324 }
1325
1326 struct drm_amdgpu_bo_list_entry *list =
1327 alloca(cs->num_real_buffers * sizeof(struct drm_amdgpu_bo_list_entry));
1328
1329 unsigned num_handles = 0;
1330 for (i = 0; i < cs->num_real_buffers; ++i) {
1331 struct amdgpu_cs_buffer *buffer = &cs->real_buffers[i];
1332
1333 if (buffer->bo->is_local)
1334 continue;
1335
1336 assert(buffer->u.real.priority_usage != 0);
1337
1338 list[num_handles].bo_handle = buffer->bo->u.real.kms_handle;
1339 list[num_handles].bo_priority = (util_last_bit(buffer->u.real.priority_usage) - 1) / 2;
1340 ++num_handles;
1341 }
1342
1343 if (use_bo_list_create) {
1344 /* Legacy path creating the buffer list handle and passing it to the CS ioctl. */
1345 r = amdgpu_bo_list_create_raw(ws->dev, num_handles, list, &bo_list);
1346 if (r) {
1347 fprintf(stderr, "amdgpu: buffer list creation failed (%d)\n", r);
1348 goto cleanup;
1349 }
1350 } else {
1351 /* Standard path passing the buffer list via the CS ioctl. */
1352 bo_list_in.operation = ~0;
1353 bo_list_in.list_handle = ~0;
1354 bo_list_in.bo_number = num_handles;
1355 bo_list_in.bo_info_size = sizeof(struct drm_amdgpu_bo_list_entry);
1356 bo_list_in.bo_info_ptr = (uint64_t)(uintptr_t)list;
1357 }
1358 }
1359
1360 if (acs->ring_type == RING_GFX)
1361 ws->gfx_bo_list_counter += cs->num_real_buffers;
1362
1363 if (acs->stop_exec_on_failure && acs->ctx->num_rejected_cs) {
1364 r = -ECANCELED;
1365 } else {
1366 struct drm_amdgpu_cs_chunk chunks[6];
1367 unsigned num_chunks = 0;
1368
1369 /* BO list */
1370 if (!use_bo_list_create) {
1371 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_BO_HANDLES;
1372 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_bo_list_in) / 4;
1373 chunks[num_chunks].chunk_data = (uintptr_t)&bo_list_in;
1374 num_chunks++;
1375 }
1376
1377 /* Fence dependencies. */
1378 unsigned num_dependencies = cs->fence_dependencies.num;
1379 if (num_dependencies) {
1380 struct drm_amdgpu_cs_chunk_dep *dep_chunk =
1381 alloca(num_dependencies * sizeof(*dep_chunk));
1382
1383 for (unsigned i = 0; i < num_dependencies; i++) {
1384 struct amdgpu_fence *fence =
1385 (struct amdgpu_fence*)cs->fence_dependencies.list[i];
1386
1387 assert(util_queue_fence_is_signalled(&fence->submitted));
1388 amdgpu_cs_chunk_fence_to_dep(&fence->fence, &dep_chunk[i]);
1389 }
1390
1391 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_DEPENDENCIES;
1392 chunks[num_chunks].length_dw = sizeof(dep_chunk[0]) / 4 * num_dependencies;
1393 chunks[num_chunks].chunk_data = (uintptr_t)dep_chunk;
1394 num_chunks++;
1395 }
1396
1397 /* Syncobj dependencies. */
1398 unsigned num_syncobj_dependencies = cs->syncobj_dependencies.num;
1399 if (num_syncobj_dependencies) {
1400 struct drm_amdgpu_cs_chunk_sem *sem_chunk =
1401 alloca(num_syncobj_dependencies * sizeof(sem_chunk[0]));
1402
1403 for (unsigned i = 0; i < num_syncobj_dependencies; i++) {
1404 struct amdgpu_fence *fence =
1405 (struct amdgpu_fence*)cs->syncobj_dependencies.list[i];
1406
1407 if (!amdgpu_fence_is_syncobj(fence))
1408 continue;
1409
1410 assert(util_queue_fence_is_signalled(&fence->submitted));
1411 sem_chunk[i].handle = fence->syncobj;
1412 }
1413
1414 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_SYNCOBJ_IN;
1415 chunks[num_chunks].length_dw = sizeof(sem_chunk[0]) / 4 * num_syncobj_dependencies;
1416 chunks[num_chunks].chunk_data = (uintptr_t)sem_chunk;
1417 num_chunks++;
1418 }
1419
1420 /* Syncobj signals. */
1421 unsigned num_syncobj_to_signal = cs->syncobj_to_signal.num;
1422 if (num_syncobj_to_signal) {
1423 struct drm_amdgpu_cs_chunk_sem *sem_chunk =
1424 alloca(num_syncobj_to_signal * sizeof(sem_chunk[0]));
1425
1426 for (unsigned i = 0; i < num_syncobj_to_signal; i++) {
1427 struct amdgpu_fence *fence =
1428 (struct amdgpu_fence*)cs->syncobj_to_signal.list[i];
1429
1430 assert(amdgpu_fence_is_syncobj(fence));
1431 sem_chunk[i].handle = fence->syncobj;
1432 }
1433
1434 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_SYNCOBJ_OUT;
1435 chunks[num_chunks].length_dw = sizeof(sem_chunk[0]) / 4
1436 * num_syncobj_to_signal;
1437 chunks[num_chunks].chunk_data = (uintptr_t)sem_chunk;
1438 num_chunks++;
1439 }
1440
1441 /* Fence */
1442 if (has_user_fence) {
1443 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_FENCE;
1444 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_cs_chunk_fence) / 4;
1445 chunks[num_chunks].chunk_data = (uintptr_t)&acs->fence_chunk;
1446 num_chunks++;
1447 }
1448
1449 /* IB */
1450 cs->ib[IB_MAIN].ib_bytes *= 4; /* Convert from dwords to bytes. */
1451 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_IB;
1452 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_cs_chunk_ib) / 4;
1453 chunks[num_chunks].chunk_data = (uintptr_t)&cs->ib[IB_MAIN];
1454 num_chunks++;
1455
1456 assert(num_chunks <= ARRAY_SIZE(chunks));
1457
1458 r = amdgpu_cs_submit_raw2(ws->dev, acs->ctx->ctx, bo_list,
1459 num_chunks, chunks, &seq_no);
1460 }
1461
1462 if (r) {
1463 if (r == -ENOMEM)
1464 fprintf(stderr, "amdgpu: Not enough memory for command submission.\n");
1465 else if (r == -ECANCELED)
1466 fprintf(stderr, "amdgpu: The CS has been cancelled because the context is lost.\n");
1467 else
1468 fprintf(stderr, "amdgpu: The CS has been rejected, "
1469 "see dmesg for more information (%i).\n", r);
1470
1471 acs->ctx->num_rejected_cs++;
1472 ws->num_total_rejected_cs++;
1473 } else {
1474 /* Success. */
1475 uint64_t *user_fence = NULL;
1476
1477 if (has_user_fence)
1478 user_fence = acs->ctx->user_fence_cpu_address_base + acs->ring_type;
1479 amdgpu_fence_submitted(cs->fence, seq_no, user_fence);
1480 }
1481
1482 /* Cleanup. */
1483 if (bo_list)
1484 amdgpu_bo_list_destroy_raw(ws->dev, bo_list);
1485
1486 cleanup:
1487 /* If there was an error, signal the fence, because it won't be signalled
1488 * by the hardware. */
1489 if (r)
1490 amdgpu_fence_signalled(cs->fence);
1491
1492 cs->error_code = r;
1493
1494 for (i = 0; i < cs->num_real_buffers; i++)
1495 p_atomic_dec(&cs->real_buffers[i].bo->num_active_ioctls);
1496 for (i = 0; i < cs->num_slab_buffers; i++)
1497 p_atomic_dec(&cs->slab_buffers[i].bo->num_active_ioctls);
1498 for (i = 0; i < cs->num_sparse_buffers; i++)
1499 p_atomic_dec(&cs->sparse_buffers[i].bo->num_active_ioctls);
1500
1501 amdgpu_cs_context_cleanup(cs);
1502 }
1503
1504 /* Make sure the previous submission is completed. */
1505 void amdgpu_cs_sync_flush(struct radeon_cmdbuf *rcs)
1506 {
1507 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1508
1509 /* Wait for any pending ioctl of this CS to complete. */
1510 util_queue_fence_wait(&cs->flush_completed);
1511 }
1512
1513 static int amdgpu_cs_flush(struct radeon_cmdbuf *rcs,
1514 unsigned flags,
1515 struct pipe_fence_handle **fence)
1516 {
1517 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1518 struct amdgpu_winsys *ws = cs->ctx->ws;
1519 int error_code = 0;
1520
1521 rcs->current.max_dw += amdgpu_cs_epilog_dws(cs);
1522
1523 switch (cs->ring_type) {
1524 case RING_DMA:
1525 /* pad DMA ring to 8 DWs */
1526 if (ws->info.chip_class <= SI) {
1527 while (rcs->current.cdw & 7)
1528 radeon_emit(rcs, 0xf0000000); /* NOP packet */
1529 } else {
1530 while (rcs->current.cdw & 7)
1531 radeon_emit(rcs, 0x00000000); /* NOP packet */
1532 }
1533 break;
1534 case RING_GFX:
1535 case RING_COMPUTE:
1536 /* pad GFX ring to 8 DWs to meet CP fetch alignment requirements */
1537 if (ws->info.gfx_ib_pad_with_type2) {
1538 while (rcs->current.cdw & 7)
1539 radeon_emit(rcs, 0x80000000); /* type2 nop packet */
1540 } else {
1541 while (rcs->current.cdw & 7)
1542 radeon_emit(rcs, 0xffff1000); /* type3 nop packet */
1543 }
1544 if (cs->ring_type == RING_GFX)
1545 ws->gfx_ib_size_counter += (rcs->prev_dw + rcs->current.cdw) * 4;
1546 break;
1547 case RING_UVD:
1548 case RING_UVD_ENC:
1549 while (rcs->current.cdw & 15)
1550 radeon_emit(rcs, 0x80000000); /* type2 nop packet */
1551 break;
1552 case RING_VCN_JPEG:
1553 if (rcs->current.cdw % 2)
1554 assert(0);
1555 while (rcs->current.cdw & 15) {
1556 radeon_emit(rcs, 0x60000000); /* nop packet */
1557 radeon_emit(rcs, 0x00000000);
1558 }
1559 break;
1560 case RING_VCN_DEC:
1561 while (rcs->current.cdw & 15)
1562 radeon_emit(rcs, 0x81ff); /* nop packet */
1563 break;
1564 default:
1565 break;
1566 }
1567
1568 if (rcs->current.cdw > rcs->current.max_dw) {
1569 fprintf(stderr, "amdgpu: command stream overflowed\n");
1570 }
1571
1572 /* If the CS is not empty or overflowed.... */
1573 if (likely(radeon_emitted(&cs->main.base, 0) &&
1574 cs->main.base.current.cdw <= cs->main.base.current.max_dw &&
1575 !debug_get_option_noop())) {
1576 struct amdgpu_cs_context *cur = cs->csc;
1577
1578 /* Set IB sizes. */
1579 amdgpu_ib_finalize(ws, &cs->main);
1580
1581 /* Create a fence. */
1582 amdgpu_fence_reference(&cur->fence, NULL);
1583 if (cs->next_fence) {
1584 /* just move the reference */
1585 cur->fence = cs->next_fence;
1586 cs->next_fence = NULL;
1587 } else {
1588 cur->fence = amdgpu_fence_create(cs->ctx,
1589 cur->ib[IB_MAIN].ip_type,
1590 cur->ib[IB_MAIN].ip_instance,
1591 cur->ib[IB_MAIN].ring);
1592 }
1593 if (fence)
1594 amdgpu_fence_reference(fence, cur->fence);
1595
1596 amdgpu_cs_sync_flush(rcs);
1597
1598 /* Prepare buffers.
1599 *
1600 * This fence must be held until the submission is queued to ensure
1601 * that the order of fence dependency updates matches the order of
1602 * submissions.
1603 */
1604 simple_mtx_lock(&ws->bo_fence_lock);
1605 amdgpu_add_fence_dependencies_bo_lists(cs);
1606
1607 /* Swap command streams. "cst" is going to be submitted. */
1608 cs->csc = cs->cst;
1609 cs->cst = cur;
1610
1611 /* Submit. */
1612 util_queue_add_job(&ws->cs_queue, cs, &cs->flush_completed,
1613 amdgpu_cs_submit_ib, NULL);
1614 /* The submission has been queued, unlock the fence now. */
1615 simple_mtx_unlock(&ws->bo_fence_lock);
1616
1617 if (!(flags & PIPE_FLUSH_ASYNC)) {
1618 amdgpu_cs_sync_flush(rcs);
1619 error_code = cur->error_code;
1620 }
1621 } else {
1622 amdgpu_cs_context_cleanup(cs->csc);
1623 }
1624
1625 amdgpu_get_new_ib(&ws->base, cs, IB_MAIN);
1626
1627 cs->main.base.used_gart = 0;
1628 cs->main.base.used_vram = 0;
1629
1630 if (cs->ring_type == RING_GFX)
1631 ws->num_gfx_IBs++;
1632 else if (cs->ring_type == RING_DMA)
1633 ws->num_sdma_IBs++;
1634
1635 return error_code;
1636 }
1637
1638 static void amdgpu_cs_destroy(struct radeon_cmdbuf *rcs)
1639 {
1640 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1641
1642 amdgpu_cs_sync_flush(rcs);
1643 util_queue_fence_destroy(&cs->flush_completed);
1644 p_atomic_dec(&cs->ctx->ws->num_cs);
1645 pb_reference(&cs->main.big_ib_buffer, NULL);
1646 FREE(cs->main.base.prev);
1647 amdgpu_destroy_cs_context(&cs->csc1);
1648 amdgpu_destroy_cs_context(&cs->csc2);
1649 amdgpu_fence_reference(&cs->next_fence, NULL);
1650 FREE(cs);
1651 }
1652
1653 static bool amdgpu_bo_is_referenced(struct radeon_cmdbuf *rcs,
1654 struct pb_buffer *_buf,
1655 enum radeon_bo_usage usage)
1656 {
1657 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1658 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)_buf;
1659
1660 return amdgpu_bo_is_referenced_by_cs_with_usage(cs, bo, usage);
1661 }
1662
1663 void amdgpu_cs_init_functions(struct amdgpu_winsys *ws)
1664 {
1665 ws->base.ctx_create = amdgpu_ctx_create;
1666 ws->base.ctx_destroy = amdgpu_ctx_destroy;
1667 ws->base.ctx_query_reset_status = amdgpu_ctx_query_reset_status;
1668 ws->base.cs_create = amdgpu_cs_create;
1669 ws->base.cs_destroy = amdgpu_cs_destroy;
1670 ws->base.cs_add_buffer = amdgpu_cs_add_buffer;
1671 ws->base.cs_validate = amdgpu_cs_validate;
1672 ws->base.cs_check_space = amdgpu_cs_check_space;
1673 ws->base.cs_get_buffer_list = amdgpu_cs_get_buffer_list;
1674 ws->base.cs_flush = amdgpu_cs_flush;
1675 ws->base.cs_get_next_fence = amdgpu_cs_get_next_fence;
1676 ws->base.cs_is_buffer_referenced = amdgpu_bo_is_referenced;
1677 ws->base.cs_sync_flush = amdgpu_cs_sync_flush;
1678 ws->base.cs_add_fence_dependency = amdgpu_cs_add_fence_dependency;
1679 ws->base.cs_add_syncobj_signal = amdgpu_cs_add_syncobj_signal;
1680 ws->base.fence_wait = amdgpu_fence_wait_rel_timeout;
1681 ws->base.fence_reference = amdgpu_fence_reference;
1682 ws->base.fence_import_syncobj = amdgpu_fence_import_syncobj;
1683 ws->base.fence_import_sync_file = amdgpu_fence_import_sync_file;
1684 ws->base.fence_export_sync_file = amdgpu_fence_export_sync_file;
1685 ws->base.export_signalled_sync_file = amdgpu_export_signalled_sync_file;
1686 }