winsys/amdgpu: simplify amdgpu_cs_add_buffer() a bit
[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 }
264
265 static bool amdgpu_cs_has_chaining(struct amdgpu_cs *cs)
266 {
267 return cs->ctx->ws->info.chip_class >= CIK &&
268 cs->ring_type == RING_GFX;
269 }
270
271 static unsigned amdgpu_cs_epilog_dws(enum ring_type ring_type)
272 {
273 if (ring_type == RING_GFX)
274 return 4; /* for chaining */
275
276 return 0;
277 }
278
279 int amdgpu_lookup_buffer(struct amdgpu_cs_context *cs, struct amdgpu_winsys_bo *bo)
280 {
281 unsigned hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
282 int i = cs->buffer_indices_hashlist[hash];
283 struct amdgpu_cs_buffer *buffers;
284 int num_buffers;
285
286 if (bo->bo) {
287 buffers = cs->real_buffers;
288 num_buffers = cs->num_real_buffers;
289 } else if (!bo->sparse) {
290 buffers = cs->slab_buffers;
291 num_buffers = cs->num_slab_buffers;
292 } else {
293 buffers = cs->sparse_buffers;
294 num_buffers = cs->num_sparse_buffers;
295 }
296
297 /* not found or found */
298 if (i < 0 || (i < num_buffers && buffers[i].bo == bo))
299 return i;
300
301 /* Hash collision, look for the BO in the list of buffers linearly. */
302 for (i = num_buffers - 1; i >= 0; i--) {
303 if (buffers[i].bo == bo) {
304 /* Put this buffer in the hash list.
305 * This will prevent additional hash collisions if there are
306 * several consecutive lookup_buffer calls for the same buffer.
307 *
308 * Example: Assuming buffers A,B,C collide in the hash list,
309 * the following sequence of buffers:
310 * AAAAAAAAAAABBBBBBBBBBBBBBCCCCCCCC
311 * will collide here: ^ and here: ^,
312 * meaning that we should get very few collisions in the end. */
313 cs->buffer_indices_hashlist[hash] = i;
314 return i;
315 }
316 }
317 return -1;
318 }
319
320 static int
321 amdgpu_do_add_real_buffer(struct amdgpu_cs_context *cs, struct amdgpu_winsys_bo *bo)
322 {
323 struct amdgpu_cs_buffer *buffer;
324 int idx;
325
326 /* New buffer, check if the backing array is large enough. */
327 if (cs->num_real_buffers >= cs->max_real_buffers) {
328 unsigned new_max =
329 MAX2(cs->max_real_buffers + 16, (unsigned)(cs->max_real_buffers * 1.3));
330 struct amdgpu_cs_buffer *new_buffers;
331
332 new_buffers = MALLOC(new_max * sizeof(*new_buffers));
333
334 if (!new_buffers) {
335 fprintf(stderr, "amdgpu_do_add_buffer: allocation failed\n");
336 FREE(new_buffers);
337 return -1;
338 }
339
340 memcpy(new_buffers, cs->real_buffers, cs->num_real_buffers * sizeof(*new_buffers));
341
342 FREE(cs->real_buffers);
343
344 cs->max_real_buffers = new_max;
345 cs->real_buffers = new_buffers;
346 }
347
348 idx = cs->num_real_buffers;
349 buffer = &cs->real_buffers[idx];
350
351 memset(buffer, 0, sizeof(*buffer));
352 amdgpu_winsys_bo_reference(&buffer->bo, bo);
353 p_atomic_inc(&bo->num_cs_references);
354 cs->num_real_buffers++;
355
356 return idx;
357 }
358
359 static int
360 amdgpu_lookup_or_add_real_buffer(struct amdgpu_cs *acs, struct amdgpu_winsys_bo *bo)
361 {
362 struct amdgpu_cs_context *cs = acs->csc;
363 unsigned hash;
364 int idx = amdgpu_lookup_buffer(cs, bo);
365
366 if (idx >= 0)
367 return idx;
368
369 idx = amdgpu_do_add_real_buffer(cs, bo);
370
371 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
372 cs->buffer_indices_hashlist[hash] = idx;
373
374 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
375 acs->main.base.used_vram += bo->base.size;
376 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
377 acs->main.base.used_gart += bo->base.size;
378
379 return idx;
380 }
381
382 static int amdgpu_lookup_or_add_slab_buffer(struct amdgpu_cs *acs,
383 struct amdgpu_winsys_bo *bo)
384 {
385 struct amdgpu_cs_context *cs = acs->csc;
386 struct amdgpu_cs_buffer *buffer;
387 unsigned hash;
388 int idx = amdgpu_lookup_buffer(cs, bo);
389 int real_idx;
390
391 if (idx >= 0)
392 return idx;
393
394 real_idx = amdgpu_lookup_or_add_real_buffer(acs, bo->u.slab.real);
395 if (real_idx < 0)
396 return -1;
397
398 /* New buffer, check if the backing array is large enough. */
399 if (cs->num_slab_buffers >= cs->max_slab_buffers) {
400 unsigned new_max =
401 MAX2(cs->max_slab_buffers + 16, (unsigned)(cs->max_slab_buffers * 1.3));
402 struct amdgpu_cs_buffer *new_buffers;
403
404 new_buffers = REALLOC(cs->slab_buffers,
405 cs->max_slab_buffers * sizeof(*new_buffers),
406 new_max * sizeof(*new_buffers));
407 if (!new_buffers) {
408 fprintf(stderr, "amdgpu_lookup_or_add_slab_buffer: allocation failed\n");
409 return -1;
410 }
411
412 cs->max_slab_buffers = new_max;
413 cs->slab_buffers = new_buffers;
414 }
415
416 idx = cs->num_slab_buffers;
417 buffer = &cs->slab_buffers[idx];
418
419 memset(buffer, 0, sizeof(*buffer));
420 amdgpu_winsys_bo_reference(&buffer->bo, bo);
421 buffer->u.slab.real_idx = real_idx;
422 p_atomic_inc(&bo->num_cs_references);
423 cs->num_slab_buffers++;
424
425 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
426 cs->buffer_indices_hashlist[hash] = idx;
427
428 return idx;
429 }
430
431 static int amdgpu_lookup_or_add_sparse_buffer(struct amdgpu_cs *acs,
432 struct amdgpu_winsys_bo *bo)
433 {
434 struct amdgpu_cs_context *cs = acs->csc;
435 struct amdgpu_cs_buffer *buffer;
436 unsigned hash;
437 int idx = amdgpu_lookup_buffer(cs, bo);
438
439 if (idx >= 0)
440 return idx;
441
442 /* New buffer, check if the backing array is large enough. */
443 if (cs->num_sparse_buffers >= cs->max_sparse_buffers) {
444 unsigned new_max =
445 MAX2(cs->max_sparse_buffers + 16, (unsigned)(cs->max_sparse_buffers * 1.3));
446 struct amdgpu_cs_buffer *new_buffers;
447
448 new_buffers = REALLOC(cs->sparse_buffers,
449 cs->max_sparse_buffers * sizeof(*new_buffers),
450 new_max * sizeof(*new_buffers));
451 if (!new_buffers) {
452 fprintf(stderr, "amdgpu_lookup_or_add_sparse_buffer: allocation failed\n");
453 return -1;
454 }
455
456 cs->max_sparse_buffers = new_max;
457 cs->sparse_buffers = new_buffers;
458 }
459
460 idx = cs->num_sparse_buffers;
461 buffer = &cs->sparse_buffers[idx];
462
463 memset(buffer, 0, sizeof(*buffer));
464 amdgpu_winsys_bo_reference(&buffer->bo, bo);
465 p_atomic_inc(&bo->num_cs_references);
466 cs->num_sparse_buffers++;
467
468 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
469 cs->buffer_indices_hashlist[hash] = idx;
470
471 /* We delay adding the backing buffers until we really have to. However,
472 * we cannot delay accounting for memory use.
473 */
474 mtx_lock(&bo->u.sparse.commit_lock);
475
476 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
477 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
478 acs->main.base.used_vram += backing->bo->base.size;
479 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
480 acs->main.base.used_gart += backing->bo->base.size;
481 }
482
483 mtx_unlock(&bo->u.sparse.commit_lock);
484
485 return idx;
486 }
487
488 static unsigned amdgpu_cs_add_buffer(struct radeon_winsys_cs *rcs,
489 struct pb_buffer *buf,
490 enum radeon_bo_usage usage,
491 enum radeon_bo_domain domains,
492 enum radeon_bo_priority priority)
493 {
494 /* Don't use the "domains" parameter. Amdgpu doesn't support changing
495 * the buffer placement during command submission.
496 */
497 struct amdgpu_cs *acs = amdgpu_cs(rcs);
498 struct amdgpu_cs_context *cs = acs->csc;
499 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
500 struct amdgpu_cs_buffer *buffer;
501 int index;
502
503 /* Fast exit for no-op calls.
504 * This is very effective with suballocators and linear uploaders that
505 * are outside of the winsys.
506 */
507 if (bo == cs->last_added_bo &&
508 (usage & cs->last_added_bo_usage) == usage &&
509 (1ull << priority) & cs->last_added_bo_priority_usage)
510 return cs->last_added_bo_index;
511
512 if (!bo->sparse) {
513 if (!bo->bo) {
514 index = amdgpu_lookup_or_add_slab_buffer(acs, bo);
515 if (index < 0)
516 return 0;
517
518 buffer = &cs->slab_buffers[index];
519 buffer->usage |= usage;
520
521 usage &= ~RADEON_USAGE_SYNCHRONIZED;
522 index = buffer->u.slab.real_idx;
523 } else {
524 index = amdgpu_lookup_or_add_real_buffer(acs, bo);
525 if (index < 0)
526 return 0;
527 }
528
529 buffer = &cs->real_buffers[index];
530 } else {
531 index = amdgpu_lookup_or_add_sparse_buffer(acs, bo);
532 if (index < 0)
533 return 0;
534
535 buffer = &cs->sparse_buffers[index];
536 }
537
538 buffer->u.real.priority_usage |= 1llu << priority;
539 buffer->usage |= usage;
540
541 cs->last_added_bo = bo;
542 cs->last_added_bo_index = index;
543 cs->last_added_bo_usage = buffer->usage;
544 cs->last_added_bo_priority_usage = buffer->u.real.priority_usage;
545 return index;
546 }
547
548 static bool amdgpu_ib_new_buffer(struct amdgpu_winsys *ws, struct amdgpu_ib *ib)
549 {
550 struct pb_buffer *pb;
551 uint8_t *mapped;
552 unsigned buffer_size;
553
554 /* Always create a buffer that is at least as large as the maximum seen IB
555 * size, aligned to a power of two (and multiplied by 4 to reduce internal
556 * fragmentation if chaining is not available). Limit to 512k dwords, which
557 * is the largest power of two that fits into the size field of the
558 * INDIRECT_BUFFER packet.
559 */
560 if (amdgpu_cs_has_chaining(amdgpu_cs_from_ib(ib)))
561 buffer_size = 4 *util_next_power_of_two(ib->max_ib_size);
562 else
563 buffer_size = 4 *util_next_power_of_two(4 * ib->max_ib_size);
564
565 buffer_size = MIN2(buffer_size, 4 * 512 * 1024);
566
567 switch (ib->ib_type) {
568 case IB_CONST_PREAMBLE:
569 buffer_size = MAX2(buffer_size, 4 * 1024);
570 break;
571 case IB_CONST:
572 buffer_size = MAX2(buffer_size, 16 * 1024 * 4);
573 break;
574 case IB_MAIN:
575 buffer_size = MAX2(buffer_size, 8 * 1024 * 4);
576 break;
577 default:
578 unreachable("unhandled IB type");
579 }
580
581 pb = ws->base.buffer_create(&ws->base, buffer_size,
582 ws->info.gart_page_size,
583 RADEON_DOMAIN_GTT,
584 RADEON_FLAG_CPU_ACCESS);
585 if (!pb)
586 return false;
587
588 mapped = ws->base.buffer_map(pb, NULL, PIPE_TRANSFER_WRITE);
589 if (!mapped) {
590 pb_reference(&pb, NULL);
591 return false;
592 }
593
594 pb_reference(&ib->big_ib_buffer, pb);
595 pb_reference(&pb, NULL);
596
597 ib->ib_mapped = mapped;
598 ib->used_ib_space = 0;
599
600 return true;
601 }
602
603 static unsigned amdgpu_ib_max_submit_dwords(enum ib_type ib_type)
604 {
605 switch (ib_type) {
606 case IB_MAIN:
607 /* Smaller submits means the GPU gets busy sooner and there is less
608 * waiting for buffers and fences. Proof:
609 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
610 */
611 return 20 * 1024;
612 case IB_CONST_PREAMBLE:
613 case IB_CONST:
614 /* There isn't really any reason to limit CE IB size beyond the natural
615 * limit implied by the main IB, except perhaps GTT size. Just return
616 * an extremely large value that we never get anywhere close to.
617 */
618 return 16 * 1024 * 1024;
619 default:
620 unreachable("bad ib_type");
621 }
622 }
623
624 static bool amdgpu_get_new_ib(struct radeon_winsys *ws, struct amdgpu_cs *cs,
625 enum ib_type ib_type)
626 {
627 struct amdgpu_winsys *aws = (struct amdgpu_winsys*)ws;
628 /* Small IBs are better than big IBs, because the GPU goes idle quicker
629 * and there is less waiting for buffers and fences. Proof:
630 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
631 */
632 struct amdgpu_ib *ib = NULL;
633 struct amdgpu_cs_ib_info *info = &cs->csc->ib[ib_type];
634 unsigned ib_size = 0;
635
636 switch (ib_type) {
637 case IB_CONST_PREAMBLE:
638 ib = &cs->const_preamble_ib;
639 ib_size = 256 * 4;
640 break;
641 case IB_CONST:
642 ib = &cs->const_ib;
643 ib_size = 8 * 1024 * 4;
644 break;
645 case IB_MAIN:
646 ib = &cs->main;
647 ib_size = 4 * 1024 * 4;
648 break;
649 default:
650 unreachable("unhandled IB type");
651 }
652
653 if (!amdgpu_cs_has_chaining(cs)) {
654 ib_size = MAX2(ib_size,
655 4 * MIN2(util_next_power_of_two(ib->max_ib_size),
656 amdgpu_ib_max_submit_dwords(ib_type)));
657 }
658
659 ib->max_ib_size = ib->max_ib_size - ib->max_ib_size / 32;
660
661 ib->base.prev_dw = 0;
662 ib->base.num_prev = 0;
663 ib->base.current.cdw = 0;
664 ib->base.current.buf = NULL;
665
666 /* Allocate a new buffer for IBs if the current buffer is all used. */
667 if (!ib->big_ib_buffer ||
668 ib->used_ib_space + ib_size > ib->big_ib_buffer->size) {
669 if (!amdgpu_ib_new_buffer(aws, ib))
670 return false;
671 }
672
673 info->ib_mc_address = amdgpu_winsys_bo(ib->big_ib_buffer)->va +
674 ib->used_ib_space;
675 info->size = 0;
676 ib->ptr_ib_size = &info->size;
677
678 amdgpu_cs_add_buffer(&cs->main.base, ib->big_ib_buffer,
679 RADEON_USAGE_READ, 0, RADEON_PRIO_IB1);
680
681 ib->base.current.buf = (uint32_t*)(ib->ib_mapped + ib->used_ib_space);
682
683 ib_size = ib->big_ib_buffer->size - ib->used_ib_space;
684 ib->base.current.max_dw = ib_size / 4 - amdgpu_cs_epilog_dws(cs->ring_type);
685 return true;
686 }
687
688 static void amdgpu_ib_finalize(struct amdgpu_ib *ib)
689 {
690 *ib->ptr_ib_size |= ib->base.current.cdw;
691 ib->used_ib_space += ib->base.current.cdw * 4;
692 ib->max_ib_size = MAX2(ib->max_ib_size, ib->base.prev_dw + ib->base.current.cdw);
693 }
694
695 static bool amdgpu_init_cs_context(struct amdgpu_cs_context *cs,
696 enum ring_type ring_type)
697 {
698 int i;
699
700 switch (ring_type) {
701 case RING_DMA:
702 cs->request.ip_type = AMDGPU_HW_IP_DMA;
703 break;
704
705 case RING_UVD:
706 cs->request.ip_type = AMDGPU_HW_IP_UVD;
707 break;
708
709 case RING_VCE:
710 cs->request.ip_type = AMDGPU_HW_IP_VCE;
711 break;
712
713 case RING_COMPUTE:
714 cs->request.ip_type = AMDGPU_HW_IP_COMPUTE;
715 break;
716
717 default:
718 case RING_GFX:
719 cs->request.ip_type = AMDGPU_HW_IP_GFX;
720 break;
721 }
722
723 for (i = 0; i < ARRAY_SIZE(cs->buffer_indices_hashlist); i++) {
724 cs->buffer_indices_hashlist[i] = -1;
725 }
726 cs->last_added_bo = NULL;
727
728 cs->request.number_of_ibs = 1;
729 cs->request.ibs = &cs->ib[IB_MAIN];
730
731 cs->ib[IB_CONST].flags = AMDGPU_IB_FLAG_CE;
732 cs->ib[IB_CONST_PREAMBLE].flags = AMDGPU_IB_FLAG_CE |
733 AMDGPU_IB_FLAG_PREAMBLE;
734
735 return true;
736 }
737
738 static void amdgpu_cs_context_cleanup(struct amdgpu_cs_context *cs)
739 {
740 unsigned i;
741
742 for (i = 0; i < cs->num_real_buffers; i++) {
743 p_atomic_dec(&cs->real_buffers[i].bo->num_cs_references);
744 amdgpu_winsys_bo_reference(&cs->real_buffers[i].bo, NULL);
745 }
746 for (i = 0; i < cs->num_slab_buffers; i++) {
747 p_atomic_dec(&cs->slab_buffers[i].bo->num_cs_references);
748 amdgpu_winsys_bo_reference(&cs->slab_buffers[i].bo, NULL);
749 }
750 for (i = 0; i < cs->num_sparse_buffers; i++) {
751 p_atomic_dec(&cs->sparse_buffers[i].bo->num_cs_references);
752 amdgpu_winsys_bo_reference(&cs->sparse_buffers[i].bo, NULL);
753 }
754
755 cs->num_real_buffers = 0;
756 cs->num_slab_buffers = 0;
757 cs->num_sparse_buffers = 0;
758 amdgpu_fence_reference(&cs->fence, NULL);
759
760 for (i = 0; i < ARRAY_SIZE(cs->buffer_indices_hashlist); i++) {
761 cs->buffer_indices_hashlist[i] = -1;
762 }
763 cs->last_added_bo = NULL;
764 }
765
766 static void amdgpu_destroy_cs_context(struct amdgpu_cs_context *cs)
767 {
768 amdgpu_cs_context_cleanup(cs);
769 FREE(cs->flags);
770 FREE(cs->real_buffers);
771 FREE(cs->handles);
772 FREE(cs->slab_buffers);
773 FREE(cs->sparse_buffers);
774 FREE(cs->request.dependencies);
775 }
776
777
778 static struct radeon_winsys_cs *
779 amdgpu_cs_create(struct radeon_winsys_ctx *rwctx,
780 enum ring_type ring_type,
781 void (*flush)(void *ctx, unsigned flags,
782 struct pipe_fence_handle **fence),
783 void *flush_ctx)
784 {
785 struct amdgpu_ctx *ctx = (struct amdgpu_ctx*)rwctx;
786 struct amdgpu_cs *cs;
787
788 cs = CALLOC_STRUCT(amdgpu_cs);
789 if (!cs) {
790 return NULL;
791 }
792
793 util_queue_fence_init(&cs->flush_completed);
794
795 cs->ctx = ctx;
796 cs->flush_cs = flush;
797 cs->flush_data = flush_ctx;
798 cs->ring_type = ring_type;
799
800 cs->main.ib_type = IB_MAIN;
801 cs->const_ib.ib_type = IB_CONST;
802 cs->const_preamble_ib.ib_type = IB_CONST_PREAMBLE;
803
804 if (!amdgpu_init_cs_context(&cs->csc1, ring_type)) {
805 FREE(cs);
806 return NULL;
807 }
808
809 if (!amdgpu_init_cs_context(&cs->csc2, ring_type)) {
810 amdgpu_destroy_cs_context(&cs->csc1);
811 FREE(cs);
812 return NULL;
813 }
814
815 /* Set the first submission context as current. */
816 cs->csc = &cs->csc1;
817 cs->cst = &cs->csc2;
818
819 if (!amdgpu_get_new_ib(&ctx->ws->base, cs, IB_MAIN)) {
820 amdgpu_destroy_cs_context(&cs->csc2);
821 amdgpu_destroy_cs_context(&cs->csc1);
822 FREE(cs);
823 return NULL;
824 }
825
826 p_atomic_inc(&ctx->ws->num_cs);
827 return &cs->main.base;
828 }
829
830 static struct radeon_winsys_cs *
831 amdgpu_cs_add_const_ib(struct radeon_winsys_cs *rcs)
832 {
833 struct amdgpu_cs *cs = (struct amdgpu_cs*)rcs;
834 struct amdgpu_winsys *ws = cs->ctx->ws;
835
836 /* only one const IB can be added */
837 if (cs->ring_type != RING_GFX || cs->const_ib.ib_mapped)
838 return NULL;
839
840 if (!amdgpu_get_new_ib(&ws->base, cs, IB_CONST))
841 return NULL;
842
843 cs->csc->request.number_of_ibs = 2;
844 cs->csc->request.ibs = &cs->csc->ib[IB_CONST];
845
846 cs->cst->request.number_of_ibs = 2;
847 cs->cst->request.ibs = &cs->cst->ib[IB_CONST];
848
849 return &cs->const_ib.base;
850 }
851
852 static struct radeon_winsys_cs *
853 amdgpu_cs_add_const_preamble_ib(struct radeon_winsys_cs *rcs)
854 {
855 struct amdgpu_cs *cs = (struct amdgpu_cs*)rcs;
856 struct amdgpu_winsys *ws = cs->ctx->ws;
857
858 /* only one const preamble IB can be added and only when the const IB has
859 * also been mapped */
860 if (cs->ring_type != RING_GFX || !cs->const_ib.ib_mapped ||
861 cs->const_preamble_ib.ib_mapped)
862 return NULL;
863
864 if (!amdgpu_get_new_ib(&ws->base, cs, IB_CONST_PREAMBLE))
865 return NULL;
866
867 cs->csc->request.number_of_ibs = 3;
868 cs->csc->request.ibs = &cs->csc->ib[IB_CONST_PREAMBLE];
869
870 cs->cst->request.number_of_ibs = 3;
871 cs->cst->request.ibs = &cs->cst->ib[IB_CONST_PREAMBLE];
872
873 return &cs->const_preamble_ib.base;
874 }
875
876 static bool amdgpu_cs_validate(struct radeon_winsys_cs *rcs)
877 {
878 return true;
879 }
880
881 static bool amdgpu_cs_check_space(struct radeon_winsys_cs *rcs, unsigned dw)
882 {
883 struct amdgpu_ib *ib = amdgpu_ib(rcs);
884 struct amdgpu_cs *cs = amdgpu_cs_from_ib(ib);
885 unsigned requested_size = rcs->prev_dw + rcs->current.cdw + dw;
886 uint64_t va;
887 uint32_t *new_ptr_ib_size;
888
889 assert(rcs->current.cdw <= rcs->current.max_dw);
890
891 if (requested_size > amdgpu_ib_max_submit_dwords(ib->ib_type))
892 return false;
893
894 ib->max_ib_size = MAX2(ib->max_ib_size, requested_size);
895
896 if (rcs->current.max_dw - rcs->current.cdw >= dw)
897 return true;
898
899 if (!amdgpu_cs_has_chaining(cs))
900 return false;
901
902 /* Allocate a new chunk */
903 if (rcs->num_prev >= rcs->max_prev) {
904 unsigned new_max_prev = MAX2(1, 2 * rcs->max_prev);
905 struct radeon_winsys_cs_chunk *new_prev;
906
907 new_prev = REALLOC(rcs->prev,
908 sizeof(*new_prev) * rcs->max_prev,
909 sizeof(*new_prev) * new_max_prev);
910 if (!new_prev)
911 return false;
912
913 rcs->prev = new_prev;
914 rcs->max_prev = new_max_prev;
915 }
916
917 if (!amdgpu_ib_new_buffer(cs->ctx->ws, ib))
918 return false;
919
920 assert(ib->used_ib_space == 0);
921 va = amdgpu_winsys_bo(ib->big_ib_buffer)->va;
922
923 /* This space was originally reserved. */
924 rcs->current.max_dw += 4;
925 assert(ib->used_ib_space + 4 * rcs->current.max_dw <= ib->big_ib_buffer->size);
926
927 /* Pad with NOPs and add INDIRECT_BUFFER packet */
928 while ((rcs->current.cdw & 7) != 4)
929 radeon_emit(rcs, 0xffff1000); /* type3 nop packet */
930
931 radeon_emit(rcs, PKT3(ib->ib_type == IB_MAIN ? PKT3_INDIRECT_BUFFER_CIK
932 : PKT3_INDIRECT_BUFFER_CONST, 2, 0));
933 radeon_emit(rcs, va);
934 radeon_emit(rcs, va >> 32);
935 new_ptr_ib_size = &rcs->current.buf[rcs->current.cdw];
936 radeon_emit(rcs, S_3F2_CHAIN(1) | S_3F2_VALID(1));
937
938 assert((rcs->current.cdw & 7) == 0);
939 assert(rcs->current.cdw <= rcs->current.max_dw);
940
941 *ib->ptr_ib_size |= rcs->current.cdw;
942 ib->ptr_ib_size = new_ptr_ib_size;
943
944 /* Hook up the new chunk */
945 rcs->prev[rcs->num_prev].buf = rcs->current.buf;
946 rcs->prev[rcs->num_prev].cdw = rcs->current.cdw;
947 rcs->prev[rcs->num_prev].max_dw = rcs->current.cdw; /* no modifications */
948 rcs->num_prev++;
949
950 ib->base.prev_dw += ib->base.current.cdw;
951 ib->base.current.cdw = 0;
952
953 ib->base.current.buf = (uint32_t*)(ib->ib_mapped + ib->used_ib_space);
954 ib->base.current.max_dw = ib->big_ib_buffer->size / 4 - amdgpu_cs_epilog_dws(cs->ring_type);
955
956 amdgpu_cs_add_buffer(&cs->main.base, ib->big_ib_buffer,
957 RADEON_USAGE_READ, 0, RADEON_PRIO_IB1);
958
959 return true;
960 }
961
962 static unsigned amdgpu_cs_get_buffer_list(struct radeon_winsys_cs *rcs,
963 struct radeon_bo_list_item *list)
964 {
965 struct amdgpu_cs_context *cs = amdgpu_cs(rcs)->csc;
966 int i;
967
968 if (list) {
969 for (i = 0; i < cs->num_real_buffers; i++) {
970 list[i].bo_size = cs->real_buffers[i].bo->base.size;
971 list[i].vm_address = cs->real_buffers[i].bo->va;
972 list[i].priority_usage = cs->real_buffers[i].u.real.priority_usage;
973 }
974 }
975 return cs->num_real_buffers;
976 }
977
978 DEBUG_GET_ONCE_BOOL_OPTION(all_bos, "RADEON_ALL_BOS", false)
979
980 static void amdgpu_add_fence_dependency(struct amdgpu_cs *acs,
981 struct amdgpu_cs_buffer *buffer)
982 {
983 struct amdgpu_cs_context *cs = acs->csc;
984 struct amdgpu_winsys_bo *bo = buffer->bo;
985 struct amdgpu_cs_fence *dep;
986 unsigned new_num_fences = 0;
987
988 for (unsigned j = 0; j < bo->num_fences; ++j) {
989 struct amdgpu_fence *bo_fence = (void *)bo->fences[j];
990 unsigned idx;
991
992 if (bo_fence->ctx == acs->ctx &&
993 bo_fence->fence.ip_type == cs->request.ip_type &&
994 bo_fence->fence.ip_instance == cs->request.ip_instance &&
995 bo_fence->fence.ring == cs->request.ring)
996 continue;
997
998 if (amdgpu_fence_wait((void *)bo_fence, 0, false))
999 continue;
1000
1001 amdgpu_fence_reference(&bo->fences[new_num_fences], bo->fences[j]);
1002 new_num_fences++;
1003
1004 if (!(buffer->usage & RADEON_USAGE_SYNCHRONIZED))
1005 continue;
1006
1007 if (bo_fence->submission_in_progress)
1008 os_wait_until_zero(&bo_fence->submission_in_progress,
1009 PIPE_TIMEOUT_INFINITE);
1010
1011 idx = cs->request.number_of_dependencies++;
1012 if (idx >= cs->max_dependencies) {
1013 unsigned size;
1014
1015 cs->max_dependencies = idx + 8;
1016 size = cs->max_dependencies * sizeof(struct amdgpu_cs_fence);
1017 cs->request.dependencies = realloc(cs->request.dependencies, size);
1018 }
1019
1020 dep = &cs->request.dependencies[idx];
1021 memcpy(dep, &bo_fence->fence, sizeof(*dep));
1022 }
1023
1024 for (unsigned j = new_num_fences; j < bo->num_fences; ++j)
1025 amdgpu_fence_reference(&bo->fences[j], NULL);
1026
1027 bo->num_fences = new_num_fences;
1028 }
1029
1030 /* Add the given list of fences to the buffer's fence list.
1031 *
1032 * Must be called with the winsys bo_fence_lock held.
1033 */
1034 void amdgpu_add_fences(struct amdgpu_winsys_bo *bo,
1035 unsigned num_fences,
1036 struct pipe_fence_handle **fences)
1037 {
1038 if (bo->num_fences + num_fences > bo->max_fences) {
1039 unsigned new_max_fences = MAX2(bo->num_fences + num_fences, bo->max_fences * 2);
1040 struct pipe_fence_handle **new_fences =
1041 REALLOC(bo->fences,
1042 bo->num_fences * sizeof(*new_fences),
1043 new_max_fences * sizeof(*new_fences));
1044 if (likely(new_fences)) {
1045 bo->fences = new_fences;
1046 bo->max_fences = new_max_fences;
1047 } else {
1048 unsigned drop;
1049
1050 fprintf(stderr, "amdgpu_add_fences: allocation failure, dropping fence(s)\n");
1051 if (!bo->num_fences)
1052 return;
1053
1054 bo->num_fences--; /* prefer to keep the most recent fence if possible */
1055 amdgpu_fence_reference(&bo->fences[bo->num_fences], NULL);
1056
1057 drop = bo->num_fences + num_fences - bo->max_fences;
1058 num_fences -= drop;
1059 fences += drop;
1060 }
1061 }
1062
1063 for (unsigned i = 0; i < num_fences; ++i) {
1064 bo->fences[bo->num_fences] = NULL;
1065 amdgpu_fence_reference(&bo->fences[bo->num_fences], fences[i]);
1066 bo->num_fences++;
1067 }
1068 }
1069
1070 static void amdgpu_add_fence_dependencies_list(struct amdgpu_cs *acs,
1071 struct pipe_fence_handle *fence,
1072 unsigned num_buffers,
1073 struct amdgpu_cs_buffer *buffers)
1074 {
1075 for (unsigned i = 0; i < num_buffers; i++) {
1076 struct amdgpu_cs_buffer *buffer = &buffers[i];
1077 struct amdgpu_winsys_bo *bo = buffer->bo;
1078
1079 amdgpu_add_fence_dependency(acs, buffer);
1080 p_atomic_inc(&bo->num_active_ioctls);
1081 amdgpu_add_fences(bo, 1, &fence);
1082 }
1083 }
1084
1085 /* Since the kernel driver doesn't synchronize execution between different
1086 * rings automatically, we have to add fence dependencies manually.
1087 */
1088 static void amdgpu_add_fence_dependencies(struct amdgpu_cs *acs)
1089 {
1090 struct amdgpu_cs_context *cs = acs->csc;
1091
1092 cs->request.number_of_dependencies = 0;
1093
1094 amdgpu_add_fence_dependencies_list(acs, cs->fence, cs->num_real_buffers, cs->real_buffers);
1095 amdgpu_add_fence_dependencies_list(acs, cs->fence, cs->num_slab_buffers, cs->slab_buffers);
1096 amdgpu_add_fence_dependencies_list(acs, cs->fence, cs->num_sparse_buffers, cs->sparse_buffers);
1097 }
1098
1099 /* Add backing of sparse buffers to the buffer list.
1100 *
1101 * This is done late, during submission, to keep the buffer list short before
1102 * submit, and to avoid managing fences for the backing buffers.
1103 */
1104 static bool amdgpu_add_sparse_backing_buffers(struct amdgpu_cs_context *cs)
1105 {
1106 for (unsigned i = 0; i < cs->num_sparse_buffers; ++i) {
1107 struct amdgpu_cs_buffer *buffer = &cs->sparse_buffers[i];
1108 struct amdgpu_winsys_bo *bo = buffer->bo;
1109
1110 mtx_lock(&bo->u.sparse.commit_lock);
1111
1112 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
1113 /* We can directly add the buffer here, because we know that each
1114 * backing buffer occurs only once.
1115 */
1116 int idx = amdgpu_do_add_real_buffer(cs, backing->bo);
1117 if (idx < 0) {
1118 fprintf(stderr, "%s: failed to add buffer\n", __FUNCTION__);
1119 mtx_unlock(&bo->u.sparse.commit_lock);
1120 return false;
1121 }
1122
1123 cs->real_buffers[idx].usage = buffer->usage & ~RADEON_USAGE_SYNCHRONIZED;
1124 cs->real_buffers[idx].u.real.priority_usage = buffer->u.real.priority_usage;
1125 p_atomic_inc(&backing->bo->num_active_ioctls);
1126 }
1127
1128 mtx_unlock(&bo->u.sparse.commit_lock);
1129 }
1130
1131 return true;
1132 }
1133
1134 void amdgpu_cs_submit_ib(void *job, int thread_index)
1135 {
1136 struct amdgpu_cs *acs = (struct amdgpu_cs*)job;
1137 struct amdgpu_winsys *ws = acs->ctx->ws;
1138 struct amdgpu_cs_context *cs = acs->cst;
1139 int i, r;
1140
1141 cs->request.fence_info.handle = NULL;
1142 if (amdgpu_cs_has_user_fence(cs)) {
1143 cs->request.fence_info.handle = acs->ctx->user_fence_bo;
1144 cs->request.fence_info.offset = acs->ring_type;
1145 }
1146
1147 /* Create the buffer list.
1148 * Use a buffer list containing all allocated buffers if requested.
1149 */
1150 if (debug_get_option_all_bos()) {
1151 struct amdgpu_winsys_bo *bo;
1152 amdgpu_bo_handle *handles;
1153 unsigned num = 0;
1154
1155 mtx_lock(&ws->global_bo_list_lock);
1156
1157 handles = malloc(sizeof(handles[0]) * ws->num_buffers);
1158 if (!handles) {
1159 mtx_unlock(&ws->global_bo_list_lock);
1160 amdgpu_cs_context_cleanup(cs);
1161 cs->error_code = -ENOMEM;
1162 return;
1163 }
1164
1165 LIST_FOR_EACH_ENTRY(bo, &ws->global_bo_list, u.real.global_list_item) {
1166 assert(num < ws->num_buffers);
1167 handles[num++] = bo->bo;
1168 }
1169
1170 r = amdgpu_bo_list_create(ws->dev, ws->num_buffers,
1171 handles, NULL,
1172 &cs->request.resources);
1173 free(handles);
1174 mtx_unlock(&ws->global_bo_list_lock);
1175 } else {
1176 if (!amdgpu_add_sparse_backing_buffers(cs)) {
1177 r = -ENOMEM;
1178 goto bo_list_error;
1179 }
1180
1181 if (cs->max_real_submit < cs->num_real_buffers) {
1182 FREE(cs->handles);
1183 FREE(cs->flags);
1184
1185 cs->handles = MALLOC(sizeof(*cs->handles) * cs->num_real_buffers);
1186 cs->flags = MALLOC(sizeof(*cs->flags) * cs->num_real_buffers);
1187
1188 if (!cs->handles || !cs->flags) {
1189 cs->max_real_submit = 0;
1190 r = -ENOMEM;
1191 goto bo_list_error;
1192 }
1193 }
1194
1195 for (i = 0; i < cs->num_real_buffers; ++i) {
1196 struct amdgpu_cs_buffer *buffer = &cs->real_buffers[i];
1197
1198 assert(buffer->u.real.priority_usage != 0);
1199
1200 cs->handles[i] = buffer->bo->bo;
1201 cs->flags[i] = (util_last_bit64(buffer->u.real.priority_usage) - 1) / 4;
1202 }
1203
1204 r = amdgpu_bo_list_create(ws->dev, cs->num_real_buffers,
1205 cs->handles, cs->flags,
1206 &cs->request.resources);
1207 }
1208 bo_list_error:
1209
1210 if (r) {
1211 fprintf(stderr, "amdgpu: buffer list creation failed (%d)\n", r);
1212 cs->request.resources = NULL;
1213 amdgpu_fence_signalled(cs->fence);
1214 cs->error_code = r;
1215 goto cleanup;
1216 }
1217
1218 if (acs->ctx->num_rejected_cs)
1219 r = -ECANCELED;
1220 else
1221 r = amdgpu_cs_submit(acs->ctx->ctx, 0, &cs->request, 1);
1222
1223 cs->error_code = r;
1224 if (r) {
1225 if (r == -ENOMEM)
1226 fprintf(stderr, "amdgpu: Not enough memory for command submission.\n");
1227 else if (r == -ECANCELED)
1228 fprintf(stderr, "amdgpu: The CS has been cancelled because the context is lost.\n");
1229 else
1230 fprintf(stderr, "amdgpu: The CS has been rejected, "
1231 "see dmesg for more information (%i).\n", r);
1232
1233 amdgpu_fence_signalled(cs->fence);
1234
1235 acs->ctx->num_rejected_cs++;
1236 ws->num_total_rejected_cs++;
1237 } else {
1238 /* Success. */
1239 uint64_t *user_fence = NULL;
1240 if (amdgpu_cs_has_user_fence(cs))
1241 user_fence = acs->ctx->user_fence_cpu_address_base +
1242 cs->request.fence_info.offset;
1243 amdgpu_fence_submitted(cs->fence, &cs->request, user_fence);
1244 }
1245
1246 /* Cleanup. */
1247 if (cs->request.resources)
1248 amdgpu_bo_list_destroy(cs->request.resources);
1249
1250 cleanup:
1251 for (i = 0; i < cs->num_real_buffers; i++)
1252 p_atomic_dec(&cs->real_buffers[i].bo->num_active_ioctls);
1253 for (i = 0; i < cs->num_slab_buffers; i++)
1254 p_atomic_dec(&cs->slab_buffers[i].bo->num_active_ioctls);
1255 for (i = 0; i < cs->num_sparse_buffers; i++)
1256 p_atomic_dec(&cs->sparse_buffers[i].bo->num_active_ioctls);
1257
1258 amdgpu_cs_context_cleanup(cs);
1259 }
1260
1261 /* Make sure the previous submission is completed. */
1262 void amdgpu_cs_sync_flush(struct radeon_winsys_cs *rcs)
1263 {
1264 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1265
1266 /* Wait for any pending ioctl of this CS to complete. */
1267 util_queue_fence_wait(&cs->flush_completed);
1268 }
1269
1270 static int amdgpu_cs_flush(struct radeon_winsys_cs *rcs,
1271 unsigned flags,
1272 struct pipe_fence_handle **fence)
1273 {
1274 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1275 struct amdgpu_winsys *ws = cs->ctx->ws;
1276 int error_code = 0;
1277
1278 rcs->current.max_dw += amdgpu_cs_epilog_dws(cs->ring_type);
1279
1280 switch (cs->ring_type) {
1281 case RING_DMA:
1282 /* pad DMA ring to 8 DWs */
1283 if (ws->info.chip_class <= SI) {
1284 while (rcs->current.cdw & 7)
1285 radeon_emit(rcs, 0xf0000000); /* NOP packet */
1286 } else {
1287 while (rcs->current.cdw & 7)
1288 radeon_emit(rcs, 0x00000000); /* NOP packet */
1289 }
1290 break;
1291 case RING_GFX:
1292 /* pad GFX ring to 8 DWs to meet CP fetch alignment requirements */
1293 if (ws->info.gfx_ib_pad_with_type2) {
1294 while (rcs->current.cdw & 7)
1295 radeon_emit(rcs, 0x80000000); /* type2 nop packet */
1296 } else {
1297 while (rcs->current.cdw & 7)
1298 radeon_emit(rcs, 0xffff1000); /* type3 nop packet */
1299 }
1300
1301 /* Also pad the const IB. */
1302 if (cs->const_ib.ib_mapped)
1303 while (!cs->const_ib.base.current.cdw || (cs->const_ib.base.current.cdw & 7))
1304 radeon_emit(&cs->const_ib.base, 0xffff1000); /* type3 nop packet */
1305
1306 if (cs->const_preamble_ib.ib_mapped)
1307 while (!cs->const_preamble_ib.base.current.cdw || (cs->const_preamble_ib.base.current.cdw & 7))
1308 radeon_emit(&cs->const_preamble_ib.base, 0xffff1000);
1309 break;
1310 case RING_UVD:
1311 while (rcs->current.cdw & 15)
1312 radeon_emit(rcs, 0x80000000); /* type2 nop packet */
1313 break;
1314 default:
1315 break;
1316 }
1317
1318 if (rcs->current.cdw > rcs->current.max_dw) {
1319 fprintf(stderr, "amdgpu: command stream overflowed\n");
1320 }
1321
1322 /* If the CS is not empty or overflowed.... */
1323 if (likely(radeon_emitted(&cs->main.base, 0) &&
1324 cs->main.base.current.cdw <= cs->main.base.current.max_dw &&
1325 !debug_get_option_noop())) {
1326 struct amdgpu_cs_context *cur = cs->csc;
1327
1328 /* Set IB sizes. */
1329 amdgpu_ib_finalize(&cs->main);
1330
1331 if (cs->const_ib.ib_mapped)
1332 amdgpu_ib_finalize(&cs->const_ib);
1333
1334 if (cs->const_preamble_ib.ib_mapped)
1335 amdgpu_ib_finalize(&cs->const_preamble_ib);
1336
1337 /* Create a fence. */
1338 amdgpu_fence_reference(&cur->fence, NULL);
1339 if (cs->next_fence) {
1340 /* just move the reference */
1341 cur->fence = cs->next_fence;
1342 cs->next_fence = NULL;
1343 } else {
1344 cur->fence = amdgpu_fence_create(cs->ctx,
1345 cur->request.ip_type,
1346 cur->request.ip_instance,
1347 cur->request.ring);
1348 }
1349 if (fence)
1350 amdgpu_fence_reference(fence, cur->fence);
1351
1352 amdgpu_cs_sync_flush(rcs);
1353
1354 /* Prepare buffers.
1355 *
1356 * This fence must be held until the submission is queued to ensure
1357 * that the order of fence dependency updates matches the order of
1358 * submissions.
1359 */
1360 mtx_lock(&ws->bo_fence_lock);
1361 amdgpu_add_fence_dependencies(cs);
1362
1363 /* Swap command streams. "cst" is going to be submitted. */
1364 cs->csc = cs->cst;
1365 cs->cst = cur;
1366
1367 /* Submit. */
1368 util_queue_add_job(&ws->cs_queue, cs, &cs->flush_completed,
1369 amdgpu_cs_submit_ib, NULL);
1370 /* The submission has been queued, unlock the fence now. */
1371 mtx_unlock(&ws->bo_fence_lock);
1372
1373 if (!(flags & RADEON_FLUSH_ASYNC)) {
1374 amdgpu_cs_sync_flush(rcs);
1375 error_code = cur->error_code;
1376 }
1377 } else {
1378 amdgpu_cs_context_cleanup(cs->csc);
1379 }
1380
1381 amdgpu_get_new_ib(&ws->base, cs, IB_MAIN);
1382 if (cs->const_ib.ib_mapped)
1383 amdgpu_get_new_ib(&ws->base, cs, IB_CONST);
1384 if (cs->const_preamble_ib.ib_mapped)
1385 amdgpu_get_new_ib(&ws->base, cs, IB_CONST_PREAMBLE);
1386
1387 cs->main.base.used_gart = 0;
1388 cs->main.base.used_vram = 0;
1389
1390 if (cs->ring_type == RING_GFX)
1391 ws->num_gfx_IBs++;
1392 else if (cs->ring_type == RING_DMA)
1393 ws->num_sdma_IBs++;
1394
1395 return error_code;
1396 }
1397
1398 static void amdgpu_cs_destroy(struct radeon_winsys_cs *rcs)
1399 {
1400 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1401
1402 amdgpu_cs_sync_flush(rcs);
1403 util_queue_fence_destroy(&cs->flush_completed);
1404 p_atomic_dec(&cs->ctx->ws->num_cs);
1405 pb_reference(&cs->main.big_ib_buffer, NULL);
1406 FREE(cs->main.base.prev);
1407 pb_reference(&cs->const_ib.big_ib_buffer, NULL);
1408 FREE(cs->const_ib.base.prev);
1409 pb_reference(&cs->const_preamble_ib.big_ib_buffer, NULL);
1410 FREE(cs->const_preamble_ib.base.prev);
1411 amdgpu_destroy_cs_context(&cs->csc1);
1412 amdgpu_destroy_cs_context(&cs->csc2);
1413 amdgpu_fence_reference(&cs->next_fence, NULL);
1414 FREE(cs);
1415 }
1416
1417 static bool amdgpu_bo_is_referenced(struct radeon_winsys_cs *rcs,
1418 struct pb_buffer *_buf,
1419 enum radeon_bo_usage usage)
1420 {
1421 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1422 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)_buf;
1423
1424 return amdgpu_bo_is_referenced_by_cs_with_usage(cs, bo, usage);
1425 }
1426
1427 void amdgpu_cs_init_functions(struct amdgpu_winsys *ws)
1428 {
1429 ws->base.ctx_create = amdgpu_ctx_create;
1430 ws->base.ctx_destroy = amdgpu_ctx_destroy;
1431 ws->base.ctx_query_reset_status = amdgpu_ctx_query_reset_status;
1432 ws->base.cs_create = amdgpu_cs_create;
1433 ws->base.cs_add_const_ib = amdgpu_cs_add_const_ib;
1434 ws->base.cs_add_const_preamble_ib = amdgpu_cs_add_const_preamble_ib;
1435 ws->base.cs_destroy = amdgpu_cs_destroy;
1436 ws->base.cs_add_buffer = amdgpu_cs_add_buffer;
1437 ws->base.cs_validate = amdgpu_cs_validate;
1438 ws->base.cs_check_space = amdgpu_cs_check_space;
1439 ws->base.cs_get_buffer_list = amdgpu_cs_get_buffer_list;
1440 ws->base.cs_flush = amdgpu_cs_flush;
1441 ws->base.cs_get_next_fence = amdgpu_cs_get_next_fence;
1442 ws->base.cs_is_buffer_referenced = amdgpu_bo_is_referenced;
1443 ws->base.cs_sync_flush = amdgpu_cs_sync_flush;
1444 ws->base.fence_wait = amdgpu_fence_wait_rel_timeout;
1445 ws->base.fence_reference = amdgpu_fence_reference;
1446 }