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