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