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