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