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