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