1 /*
2 * Copyright © 2008 Jérôme Glisse
3 * Copyright © 2010 Marek Olšák <maraeo@gmail.com>
4 * Copyright © 2015 Advanced Micro Devices, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
19 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * The above copyright notice and this permission notice (including the
25 * next paragraph) shall be included in all copies or substantial portions
26 * of the Software.
27 */
28
29 #include "amdgpu_cs.h"
30 #include "util/os_time.h"
31 #include <inttypes.h>
32 #include <stdio.h>
33
34 #include "amd/common/sid.h"
35
36 DEBUG_GET_ONCE_BOOL_OPTION(noop, "RADEON_NOOP", false)
37
38 /* FENCES */
39
40 static struct pipe_fence_handle *
amdgpu_fence_create(struct amdgpu_ctx * ctx,unsigned ip_type,unsigned ip_instance,unsigned ring)41 amdgpu_fence_create(struct amdgpu_ctx *ctx, unsigned ip_type,
42 unsigned ip_instance, unsigned ring)
43 {
44 struct amdgpu_fence *fence = CALLOC_STRUCT(amdgpu_fence);
45
46 fence->reference.count = 1;
47 fence->ws = ctx->ws;
48 fence->ctx = ctx;
49 fence->fence.context = ctx->ctx;
50 fence->fence.ip_type = ip_type;
51 fence->fence.ip_instance = ip_instance;
52 fence->fence.ring = ring;
53 util_queue_fence_init(&fence->submitted);
54 util_queue_fence_reset(&fence->submitted);
55 p_atomic_inc(&ctx->refcount);
56 return (struct pipe_fence_handle *)fence;
57 }
58
59 static struct pipe_fence_handle *
amdgpu_fence_import_syncobj(struct radeon_winsys * rws,int fd)60 amdgpu_fence_import_syncobj(struct radeon_winsys *rws, int fd)
61 {
62 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
63 struct amdgpu_fence *fence = CALLOC_STRUCT(amdgpu_fence);
64 int r;
65
66 if (!fence)
67 return NULL;
68
69 pipe_reference_init(&fence->reference, 1);
70 fence->ws = ws;
71
72 r = amdgpu_cs_import_syncobj(ws->dev, fd, &fence->syncobj);
73 if (r) {
74 FREE(fence);
75 return NULL;
76 }
77
78 util_queue_fence_init(&fence->submitted);
79
80 assert(amdgpu_fence_is_syncobj(fence));
81 return (struct pipe_fence_handle*)fence;
82 }
83
84 static struct pipe_fence_handle *
amdgpu_fence_import_sync_file(struct radeon_winsys * rws,int fd)85 amdgpu_fence_import_sync_file(struct radeon_winsys *rws, int fd)
86 {
87 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
88 struct amdgpu_fence *fence = CALLOC_STRUCT(amdgpu_fence);
89
90 if (!fence)
91 return NULL;
92
93 pipe_reference_init(&fence->reference, 1);
94 fence->ws = ws;
95 /* fence->ctx == NULL means that the fence is syncobj-based. */
96
97 /* Convert sync_file into syncobj. */
98 int r = amdgpu_cs_create_syncobj(ws->dev, &fence->syncobj);
99 if (r) {
100 FREE(fence);
101 return NULL;
102 }
103
104 r = amdgpu_cs_syncobj_import_sync_file(ws->dev, fence->syncobj, fd);
105 if (r) {
106 amdgpu_cs_destroy_syncobj(ws->dev, fence->syncobj);
107 FREE(fence);
108 return NULL;
109 }
110
111 util_queue_fence_init(&fence->submitted);
112
113 return (struct pipe_fence_handle*)fence;
114 }
115
amdgpu_fence_export_sync_file(struct radeon_winsys * rws,struct pipe_fence_handle * pfence)116 static int amdgpu_fence_export_sync_file(struct radeon_winsys *rws,
117 struct pipe_fence_handle *pfence)
118 {
119 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
120 struct amdgpu_fence *fence = (struct amdgpu_fence*)pfence;
121
122 if (amdgpu_fence_is_syncobj(fence)) {
123 int fd, r;
124
125 /* Convert syncobj into sync_file. */
126 r = amdgpu_cs_syncobj_export_sync_file(ws->dev, fence->syncobj, &fd);
127 return r ? -1 : fd;
128 }
129
130 util_queue_fence_wait(&fence->submitted);
131
132 /* Convert the amdgpu fence into a fence FD. */
133 int fd;
134 if (amdgpu_cs_fence_to_handle(ws->dev, &fence->fence,
135 AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD,
136 (uint32_t*)&fd))
137 return -1;
138
139 return fd;
140 }
141
amdgpu_export_signalled_sync_file(struct radeon_winsys * rws)142 static int amdgpu_export_signalled_sync_file(struct radeon_winsys *rws)
143 {
144 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
145 uint32_t syncobj;
146 int fd = -1;
147
148 int r = amdgpu_cs_create_syncobj2(ws->dev, DRM_SYNCOBJ_CREATE_SIGNALED,
149 &syncobj);
150 if (r) {
151 return -1;
152 }
153
154 r = amdgpu_cs_syncobj_export_sync_file(ws->dev, syncobj, &fd);
155 if (r) {
156 fd = -1;
157 }
158
159 amdgpu_cs_destroy_syncobj(ws->dev, syncobj);
160 return fd;
161 }
162
amdgpu_fence_submitted(struct pipe_fence_handle * fence,uint64_t seq_no,uint64_t * user_fence_cpu_address)163 static void amdgpu_fence_submitted(struct pipe_fence_handle *fence,
164 uint64_t seq_no,
165 uint64_t *user_fence_cpu_address)
166 {
167 struct amdgpu_fence *afence = (struct amdgpu_fence*)fence;
168
169 afence->fence.fence = seq_no;
170 afence->user_fence_cpu_address = user_fence_cpu_address;
171 util_queue_fence_signal(&afence->submitted);
172 }
173
amdgpu_fence_signalled(struct pipe_fence_handle * fence)174 static void amdgpu_fence_signalled(struct pipe_fence_handle *fence)
175 {
176 struct amdgpu_fence *afence = (struct amdgpu_fence*)fence;
177
178 afence->signalled = true;
179 util_queue_fence_signal(&afence->submitted);
180 }
181
amdgpu_fence_wait(struct pipe_fence_handle * fence,uint64_t timeout,bool absolute)182 bool amdgpu_fence_wait(struct pipe_fence_handle *fence, uint64_t timeout,
183 bool absolute)
184 {
185 struct amdgpu_fence *afence = (struct amdgpu_fence*)fence;
186 uint32_t expired;
187 int64_t abs_timeout;
188 uint64_t *user_fence_cpu;
189 int r;
190
191 if (afence->signalled)
192 return true;
193
194 if (absolute)
195 abs_timeout = timeout;
196 else
197 abs_timeout = os_time_get_absolute_timeout(timeout);
198
199 /* Handle syncobjs. */
200 if (amdgpu_fence_is_syncobj(afence)) {
201 if (abs_timeout == OS_TIMEOUT_INFINITE)
202 abs_timeout = INT64_MAX;
203
204 if (amdgpu_cs_syncobj_wait(afence->ws->dev, &afence->syncobj, 1,
205 abs_timeout, 0, NULL))
206 return false;
207
208 afence->signalled = true;
209 return true;
210 }
211
212 /* The fence might not have a number assigned if its IB is being
213 * submitted in the other thread right now. Wait until the submission
214 * is done. */
215 if (!util_queue_fence_wait_timeout(&afence->submitted, abs_timeout))
216 return false;
217
218 user_fence_cpu = afence->user_fence_cpu_address;
219 if (user_fence_cpu) {
220 if (*user_fence_cpu >= afence->fence.fence) {
221 afence->signalled = true;
222 return true;
223 }
224
225 /* No timeout, just query: no need for the ioctl. */
226 if (!absolute && !timeout)
227 return false;
228 }
229
230 /* Now use the libdrm query. */
231 r = amdgpu_cs_query_fence_status(&afence->fence,
232 abs_timeout,
233 AMDGPU_QUERY_FENCE_TIMEOUT_IS_ABSOLUTE,
234 &expired);
235 if (r) {
236 fprintf(stderr, "amdgpu: amdgpu_cs_query_fence_status failed.\n");
237 return false;
238 }
239
240 if (expired) {
241 /* This variable can only transition from false to true, so it doesn't
242 * matter if threads race for it. */
243 afence->signalled = true;
244 return true;
245 }
246 return false;
247 }
248
amdgpu_fence_wait_rel_timeout(struct radeon_winsys * rws,struct pipe_fence_handle * fence,uint64_t timeout)249 static bool amdgpu_fence_wait_rel_timeout(struct radeon_winsys *rws,
250 struct pipe_fence_handle *fence,
251 uint64_t timeout)
252 {
253 return amdgpu_fence_wait(fence, timeout, false);
254 }
255
256 static struct pipe_fence_handle *
amdgpu_cs_get_next_fence(struct radeon_cmdbuf * rcs)257 amdgpu_cs_get_next_fence(struct radeon_cmdbuf *rcs)
258 {
259 struct amdgpu_cs *cs = amdgpu_cs(rcs);
260 struct pipe_fence_handle *fence = NULL;
261
262 if (debug_get_option_noop())
263 return NULL;
264
265 if (cs->next_fence) {
266 amdgpu_fence_reference(&fence, cs->next_fence);
267 return fence;
268 }
269
270 fence = amdgpu_fence_create(cs->ctx,
271 cs->csc->ib[IB_MAIN].ip_type,
272 cs->csc->ib[IB_MAIN].ip_instance,
273 cs->csc->ib[IB_MAIN].ring);
274 if (!fence)
275 return NULL;
276
277 amdgpu_fence_reference(&cs->next_fence, fence);
278 return fence;
279 }
280
281 /* CONTEXTS */
282
amdgpu_ctx_create(struct radeon_winsys * ws)283 static struct radeon_winsys_ctx *amdgpu_ctx_create(struct radeon_winsys *ws)
284 {
285 struct amdgpu_ctx *ctx = CALLOC_STRUCT(amdgpu_ctx);
286 int r;
287 struct amdgpu_bo_alloc_request alloc_buffer = {};
288 amdgpu_bo_handle buf_handle;
289
290 if (!ctx)
291 return NULL;
292
293 ctx->ws = amdgpu_winsys(ws);
294 ctx->refcount = 1;
295 ctx->initial_num_total_rejected_cs = ctx->ws->num_total_rejected_cs;
296
297 r = amdgpu_cs_ctx_create(ctx->ws->dev, &ctx->ctx);
298 if (r) {
299 fprintf(stderr, "amdgpu: amdgpu_cs_ctx_create failed. (%i)\n", r);
300 goto error_create;
301 }
302
303 alloc_buffer.alloc_size = ctx->ws->info.gart_page_size;
304 alloc_buffer.phys_alignment = ctx->ws->info.gart_page_size;
305 alloc_buffer.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
306
307 r = amdgpu_bo_alloc(ctx->ws->dev, &alloc_buffer, &buf_handle);
308 if (r) {
309 fprintf(stderr, "amdgpu: amdgpu_bo_alloc failed. (%i)\n", r);
310 goto error_user_fence_alloc;
311 }
312
313 r = amdgpu_bo_cpu_map(buf_handle, (void**)&ctx->user_fence_cpu_address_base);
314 if (r) {
315 fprintf(stderr, "amdgpu: amdgpu_bo_cpu_map failed. (%i)\n", r);
316 goto error_user_fence_map;
317 }
318
319 memset(ctx->user_fence_cpu_address_base, 0, alloc_buffer.alloc_size);
320 ctx->user_fence_bo = buf_handle;
321
322 return (struct radeon_winsys_ctx*)ctx;
323
324 error_user_fence_map:
325 amdgpu_bo_free(buf_handle);
326 error_user_fence_alloc:
327 amdgpu_cs_ctx_free(ctx->ctx);
328 error_create:
329 FREE(ctx);
330 return NULL;
331 }
332
amdgpu_ctx_destroy(struct radeon_winsys_ctx * rwctx)333 static void amdgpu_ctx_destroy(struct radeon_winsys_ctx *rwctx)
334 {
335 amdgpu_ctx_unref((struct amdgpu_ctx*)rwctx);
336 }
337
338 static enum pipe_reset_status
amdgpu_ctx_query_reset_status(struct radeon_winsys_ctx * rwctx)339 amdgpu_ctx_query_reset_status(struct radeon_winsys_ctx *rwctx)
340 {
341 struct amdgpu_ctx *ctx = (struct amdgpu_ctx*)rwctx;
342 int r;
343
344 /* Return a failure due to a GPU hang. */
345 if (ctx->ws->info.drm_minor >= 24) {
346 uint64_t flags;
347
348 r = amdgpu_cs_query_reset_state2(ctx->ctx, &flags);
349 if (r) {
350 fprintf(stderr, "amdgpu: amdgpu_cs_query_reset_state failed. (%i)\n", r);
351 return PIPE_NO_RESET;
352 }
353
354 if (flags & AMDGPU_CTX_QUERY2_FLAGS_RESET) {
355 if (flags & AMDGPU_CTX_QUERY2_FLAGS_GUILTY)
356 return PIPE_GUILTY_CONTEXT_RESET;
357 else
358 return PIPE_INNOCENT_CONTEXT_RESET;
359 }
360 } else {
361 uint32_t result, hangs;
362
363 r = amdgpu_cs_query_reset_state(ctx->ctx, &result, &hangs);
364 if (r) {
365 fprintf(stderr, "amdgpu: amdgpu_cs_query_reset_state failed. (%i)\n", r);
366 return PIPE_NO_RESET;
367 }
368
369 switch (result) {
370 case AMDGPU_CTX_GUILTY_RESET:
371 return PIPE_GUILTY_CONTEXT_RESET;
372 case AMDGPU_CTX_INNOCENT_RESET:
373 return PIPE_INNOCENT_CONTEXT_RESET;
374 case AMDGPU_CTX_UNKNOWN_RESET:
375 return PIPE_UNKNOWN_CONTEXT_RESET;
376 }
377 }
378
379 /* Return a failure due to a rejected command submission. */
380 if (ctx->ws->num_total_rejected_cs > ctx->initial_num_total_rejected_cs) {
381 return ctx->num_rejected_cs ? PIPE_GUILTY_CONTEXT_RESET :
382 PIPE_INNOCENT_CONTEXT_RESET;
383 }
384 return PIPE_NO_RESET;
385 }
386
387 /* COMMAND SUBMISSION */
388
amdgpu_cs_has_user_fence(struct amdgpu_cs_context * cs)389 static bool amdgpu_cs_has_user_fence(struct amdgpu_cs_context *cs)
390 {
391 return cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_UVD &&
392 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCE &&
393 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_UVD_ENC &&
394 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCN_DEC &&
395 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCN_ENC &&
396 cs->ib[IB_MAIN].ip_type != AMDGPU_HW_IP_VCN_JPEG;
397 }
398
amdgpu_cs_has_chaining(struct amdgpu_cs * cs)399 static bool amdgpu_cs_has_chaining(struct amdgpu_cs *cs)
400 {
401 return cs->ctx->ws->info.chip_class >= GFX7 &&
402 (cs->ring_type == RING_GFX || cs->ring_type == RING_COMPUTE);
403 }
404
amdgpu_cs_epilog_dws(struct amdgpu_cs * cs)405 static unsigned amdgpu_cs_epilog_dws(struct amdgpu_cs *cs)
406 {
407 if (amdgpu_cs_has_chaining(cs))
408 return 4; /* for chaining */
409
410 return 0;
411 }
412
amdgpu_lookup_buffer(struct amdgpu_cs_context * cs,struct amdgpu_winsys_bo * bo)413 int amdgpu_lookup_buffer(struct amdgpu_cs_context *cs, struct amdgpu_winsys_bo *bo)
414 {
415 unsigned hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
416 int i = cs->buffer_indices_hashlist[hash];
417 struct amdgpu_cs_buffer *buffers;
418 int num_buffers;
419
420 if (bo->bo) {
421 buffers = cs->real_buffers;
422 num_buffers = cs->num_real_buffers;
423 } else if (!bo->sparse) {
424 buffers = cs->slab_buffers;
425 num_buffers = cs->num_slab_buffers;
426 } else {
427 buffers = cs->sparse_buffers;
428 num_buffers = cs->num_sparse_buffers;
429 }
430
431 /* not found or found */
432 if (i < 0 || (i < num_buffers && buffers[i].bo == bo))
433 return i;
434
435 /* Hash collision, look for the BO in the list of buffers linearly. */
436 for (i = num_buffers - 1; i >= 0; i--) {
437 if (buffers[i].bo == bo) {
438 /* Put this buffer in the hash list.
439 * This will prevent additional hash collisions if there are
440 * several consecutive lookup_buffer calls for the same buffer.
441 *
442 * Example: Assuming buffers A,B,C collide in the hash list,
443 * the following sequence of buffers:
444 * AAAAAAAAAAABBBBBBBBBBBBBBCCCCCCCC
445 * will collide here: ^ and here: ^,
446 * meaning that we should get very few collisions in the end. */
447 cs->buffer_indices_hashlist[hash] = i;
448 return i;
449 }
450 }
451 return -1;
452 }
453
454 static int
amdgpu_do_add_real_buffer(struct amdgpu_cs_context * cs,struct amdgpu_winsys_bo * bo)455 amdgpu_do_add_real_buffer(struct amdgpu_cs_context *cs, struct amdgpu_winsys_bo *bo)
456 {
457 struct amdgpu_cs_buffer *buffer;
458 int idx;
459
460 /* New buffer, check if the backing array is large enough. */
461 if (cs->num_real_buffers >= cs->max_real_buffers) {
462 unsigned new_max =
463 MAX2(cs->max_real_buffers + 16, (unsigned)(cs->max_real_buffers * 1.3));
464 struct amdgpu_cs_buffer *new_buffers;
465
466 new_buffers = MALLOC(new_max * sizeof(*new_buffers));
467
468 if (!new_buffers) {
469 fprintf(stderr, "amdgpu_do_add_buffer: allocation failed\n");
470 FREE(new_buffers);
471 return -1;
472 }
473
474 memcpy(new_buffers, cs->real_buffers, cs->num_real_buffers * sizeof(*new_buffers));
475
476 FREE(cs->real_buffers);
477
478 cs->max_real_buffers = new_max;
479 cs->real_buffers = new_buffers;
480 }
481
482 idx = cs->num_real_buffers;
483 buffer = &cs->real_buffers[idx];
484
485 memset(buffer, 0, sizeof(*buffer));
486 amdgpu_winsys_bo_reference(&buffer->bo, bo);
487 p_atomic_inc(&bo->num_cs_references);
488 cs->num_real_buffers++;
489
490 return idx;
491 }
492
493 static int
amdgpu_lookup_or_add_real_buffer(struct amdgpu_cs * acs,struct amdgpu_winsys_bo * bo)494 amdgpu_lookup_or_add_real_buffer(struct amdgpu_cs *acs, struct amdgpu_winsys_bo *bo)
495 {
496 struct amdgpu_cs_context *cs = acs->csc;
497 unsigned hash;
498 int idx = amdgpu_lookup_buffer(cs, bo);
499
500 if (idx >= 0)
501 return idx;
502
503 idx = amdgpu_do_add_real_buffer(cs, bo);
504
505 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
506 cs->buffer_indices_hashlist[hash] = idx;
507
508 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
509 acs->main.base.used_vram += bo->base.size;
510 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
511 acs->main.base.used_gart += bo->base.size;
512
513 return idx;
514 }
515
amdgpu_lookup_or_add_slab_buffer(struct amdgpu_cs * acs,struct amdgpu_winsys_bo * bo)516 static int amdgpu_lookup_or_add_slab_buffer(struct amdgpu_cs *acs,
517 struct amdgpu_winsys_bo *bo)
518 {
519 struct amdgpu_cs_context *cs = acs->csc;
520 struct amdgpu_cs_buffer *buffer;
521 unsigned hash;
522 int idx = amdgpu_lookup_buffer(cs, bo);
523 int real_idx;
524
525 if (idx >= 0)
526 return idx;
527
528 real_idx = amdgpu_lookup_or_add_real_buffer(acs, bo->u.slab.real);
529 if (real_idx < 0)
530 return -1;
531
532 /* New buffer, check if the backing array is large enough. */
533 if (cs->num_slab_buffers >= cs->max_slab_buffers) {
534 unsigned new_max =
535 MAX2(cs->max_slab_buffers + 16, (unsigned)(cs->max_slab_buffers * 1.3));
536 struct amdgpu_cs_buffer *new_buffers;
537
538 new_buffers = REALLOC(cs->slab_buffers,
539 cs->max_slab_buffers * sizeof(*new_buffers),
540 new_max * sizeof(*new_buffers));
541 if (!new_buffers) {
542 fprintf(stderr, "amdgpu_lookup_or_add_slab_buffer: allocation failed\n");
543 return -1;
544 }
545
546 cs->max_slab_buffers = new_max;
547 cs->slab_buffers = new_buffers;
548 }
549
550 idx = cs->num_slab_buffers;
551 buffer = &cs->slab_buffers[idx];
552
553 memset(buffer, 0, sizeof(*buffer));
554 amdgpu_winsys_bo_reference(&buffer->bo, bo);
555 buffer->u.slab.real_idx = real_idx;
556 p_atomic_inc(&bo->num_cs_references);
557 cs->num_slab_buffers++;
558
559 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
560 cs->buffer_indices_hashlist[hash] = idx;
561
562 return idx;
563 }
564
amdgpu_lookup_or_add_sparse_buffer(struct amdgpu_cs * acs,struct amdgpu_winsys_bo * bo)565 static int amdgpu_lookup_or_add_sparse_buffer(struct amdgpu_cs *acs,
566 struct amdgpu_winsys_bo *bo)
567 {
568 struct amdgpu_cs_context *cs = acs->csc;
569 struct amdgpu_cs_buffer *buffer;
570 unsigned hash;
571 int idx = amdgpu_lookup_buffer(cs, bo);
572
573 if (idx >= 0)
574 return idx;
575
576 /* New buffer, check if the backing array is large enough. */
577 if (cs->num_sparse_buffers >= cs->max_sparse_buffers) {
578 unsigned new_max =
579 MAX2(cs->max_sparse_buffers + 16, (unsigned)(cs->max_sparse_buffers * 1.3));
580 struct amdgpu_cs_buffer *new_buffers;
581
582 new_buffers = REALLOC(cs->sparse_buffers,
583 cs->max_sparse_buffers * sizeof(*new_buffers),
584 new_max * sizeof(*new_buffers));
585 if (!new_buffers) {
586 fprintf(stderr, "amdgpu_lookup_or_add_sparse_buffer: allocation failed\n");
587 return -1;
588 }
589
590 cs->max_sparse_buffers = new_max;
591 cs->sparse_buffers = new_buffers;
592 }
593
594 idx = cs->num_sparse_buffers;
595 buffer = &cs->sparse_buffers[idx];
596
597 memset(buffer, 0, sizeof(*buffer));
598 amdgpu_winsys_bo_reference(&buffer->bo, bo);
599 p_atomic_inc(&bo->num_cs_references);
600 cs->num_sparse_buffers++;
601
602 hash = bo->unique_id & (ARRAY_SIZE(cs->buffer_indices_hashlist)-1);
603 cs->buffer_indices_hashlist[hash] = idx;
604
605 /* We delay adding the backing buffers until we really have to. However,
606 * we cannot delay accounting for memory use.
607 */
608 simple_mtx_lock(&bo->lock);
609
610 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
611 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
612 acs->main.base.used_vram += backing->bo->base.size;
613 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
614 acs->main.base.used_gart += backing->bo->base.size;
615 }
616
617 simple_mtx_unlock(&bo->lock);
618
619 return idx;
620 }
621
amdgpu_cs_add_buffer(struct radeon_cmdbuf * rcs,struct pb_buffer * buf,enum radeon_bo_usage usage,enum radeon_bo_domain domains,enum radeon_bo_priority priority)622 static unsigned amdgpu_cs_add_buffer(struct radeon_cmdbuf *rcs,
623 struct pb_buffer *buf,
624 enum radeon_bo_usage usage,
625 enum radeon_bo_domain domains,
626 enum radeon_bo_priority priority)
627 {
628 /* Don't use the "domains" parameter. Amdgpu doesn't support changing
629 * the buffer placement during command submission.
630 */
631 struct amdgpu_cs *acs = amdgpu_cs(rcs);
632 struct amdgpu_cs_context *cs = acs->csc;
633 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
634 struct amdgpu_cs_buffer *buffer;
635 int index;
636
637 /* Fast exit for no-op calls.
638 * This is very effective with suballocators and linear uploaders that
639 * are outside of the winsys.
640 */
641 if (bo == cs->last_added_bo &&
642 (usage & cs->last_added_bo_usage) == usage &&
643 (1u << priority) & cs->last_added_bo_priority_usage)
644 return cs->last_added_bo_index;
645
646 if (!bo->sparse) {
647 if (!bo->bo) {
648 index = amdgpu_lookup_or_add_slab_buffer(acs, bo);
649 if (index < 0)
650 return 0;
651
652 buffer = &cs->slab_buffers[index];
653 buffer->usage |= usage;
654
655 usage &= ~RADEON_USAGE_SYNCHRONIZED;
656 index = buffer->u.slab.real_idx;
657 } else {
658 index = amdgpu_lookup_or_add_real_buffer(acs, bo);
659 if (index < 0)
660 return 0;
661 }
662
663 buffer = &cs->real_buffers[index];
664 } else {
665 index = amdgpu_lookup_or_add_sparse_buffer(acs, bo);
666 if (index < 0)
667 return 0;
668
669 buffer = &cs->sparse_buffers[index];
670 }
671
672 buffer->u.real.priority_usage |= 1u << priority;
673 buffer->usage |= usage;
674
675 cs->last_added_bo = bo;
676 cs->last_added_bo_index = index;
677 cs->last_added_bo_usage = buffer->usage;
678 cs->last_added_bo_priority_usage = buffer->u.real.priority_usage;
679 return index;
680 }
681
amdgpu_ib_new_buffer(struct amdgpu_winsys * ws,struct amdgpu_ib * ib,enum ring_type ring_type)682 static bool amdgpu_ib_new_buffer(struct amdgpu_winsys *ws,
683 struct amdgpu_ib *ib,
684 enum ring_type ring_type)
685 {
686 struct pb_buffer *pb;
687 uint8_t *mapped;
688 unsigned buffer_size;
689
690 /* Always create a buffer that is at least as large as the maximum seen IB
691 * size, aligned to a power of two (and multiplied by 4 to reduce internal
692 * fragmentation if chaining is not available). Limit to 512k dwords, which
693 * is the largest power of two that fits into the size field of the
694 * INDIRECT_BUFFER packet.
695 */
696 if (amdgpu_cs_has_chaining(amdgpu_cs_from_ib(ib)))
697 buffer_size = 4 *util_next_power_of_two(ib->max_ib_size);
698 else
699 buffer_size = 4 *util_next_power_of_two(4 * ib->max_ib_size);
700
701 const unsigned min_size = MAX2(ib->max_check_space_size, 8 * 1024 * 4);
702 const unsigned max_size = 512 * 1024 * 4;
703
704 buffer_size = MIN2(buffer_size, max_size);
705 buffer_size = MAX2(buffer_size, min_size); /* min_size is more important */
706
707 pb = amdgpu_bo_create(ws, buffer_size,
708 ws->info.gart_page_size,
709 RADEON_DOMAIN_GTT,
710 RADEON_FLAG_NO_INTERPROCESS_SHARING |
711 (ring_type == RING_GFX ||
712 ring_type == RING_COMPUTE ||
713 ring_type == RING_DMA ?
714 RADEON_FLAG_32BIT | RADEON_FLAG_GTT_WC : 0));
715 if (!pb)
716 return false;
717
718 mapped = amdgpu_bo_map(pb, NULL, PIPE_MAP_WRITE);
719 if (!mapped) {
720 pb_reference(&pb, NULL);
721 return false;
722 }
723
724 pb_reference(&ib->big_ib_buffer, pb);
725 pb_reference(&pb, NULL);
726
727 ib->ib_mapped = mapped;
728 ib->used_ib_space = 0;
729
730 return true;
731 }
732
amdgpu_ib_max_submit_dwords(enum ib_type ib_type)733 static unsigned amdgpu_ib_max_submit_dwords(enum ib_type ib_type)
734 {
735 /* The maximum IB size including all chained IBs. */
736 switch (ib_type) {
737 case IB_MAIN:
738 /* Smaller submits means the GPU gets busy sooner and there is less
739 * waiting for buffers and fences. Proof:
740 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
741 */
742 return 20 * 1024;
743 case IB_PARALLEL_COMPUTE:
744 /* Always chain this IB. */
745 return UINT_MAX;
746 default:
747 unreachable("bad ib_type");
748 }
749 }
750
amdgpu_get_new_ib(struct amdgpu_winsys * ws,struct amdgpu_cs * cs,enum ib_type ib_type)751 static bool amdgpu_get_new_ib(struct amdgpu_winsys *ws, struct amdgpu_cs *cs,
752 enum ib_type ib_type)
753 {
754 /* Small IBs are better than big IBs, because the GPU goes idle quicker
755 * and there is less waiting for buffers and fences. Proof:
756 * http://www.phoronix.com/scan.php?page=article&item=mesa-111-si&num=1
757 */
758 struct amdgpu_ib *ib = NULL;
759 struct drm_amdgpu_cs_chunk_ib *info = &cs->csc->ib[ib_type];
760 /* This is the minimum size of a contiguous IB. */
761 unsigned ib_size = 4 * 1024 * 4;
762
763 switch (ib_type) {
764 case IB_PARALLEL_COMPUTE:
765 ib = &cs->compute_ib;
766 break;
767 case IB_MAIN:
768 ib = &cs->main;
769 break;
770 default:
771 unreachable("unhandled IB type");
772 }
773
774 /* Always allocate at least the size of the biggest cs_check_space call,
775 * because precisely the last call might have requested this size.
776 */
777 ib_size = MAX2(ib_size, ib->max_check_space_size);
778
779 if (!amdgpu_cs_has_chaining(cs)) {
780 ib_size = MAX2(ib_size,
781 4 * MIN2(util_next_power_of_two(ib->max_ib_size),
782 amdgpu_ib_max_submit_dwords(ib_type)));
783 }
784
785 ib->max_ib_size = ib->max_ib_size - ib->max_ib_size / 32;
786
787 ib->base.prev_dw = 0;
788 ib->base.num_prev = 0;
789 ib->base.current.cdw = 0;
790 ib->base.current.buf = NULL;
791
792 /* Allocate a new buffer for IBs if the current buffer is all used. */
793 if (!ib->big_ib_buffer ||
794 ib->used_ib_space + ib_size > ib->big_ib_buffer->size) {
795 if (!amdgpu_ib_new_buffer(ws, ib, cs->ring_type))
796 return false;
797 }
798
799 info->va_start = amdgpu_winsys_bo(ib->big_ib_buffer)->va + ib->used_ib_space;
800 info->ib_bytes = 0;
801 /* ib_bytes is in dwords and the conversion to bytes will be done before
802 * the CS ioctl. */
803 ib->ptr_ib_size = &info->ib_bytes;
804 ib->ptr_ib_size_inside_ib = false;
805
806 amdgpu_cs_add_buffer(&cs->main.base, ib->big_ib_buffer,
807 RADEON_USAGE_READ, 0, RADEON_PRIO_IB1);
808
809 ib->base.current.buf = (uint32_t*)(ib->ib_mapped + ib->used_ib_space);
810
811 ib_size = ib->big_ib_buffer->size - ib->used_ib_space;
812 ib->base.current.max_dw = ib_size / 4 - amdgpu_cs_epilog_dws(cs);
813 ib->base.gpu_address = info->va_start;
814 return true;
815 }
816
amdgpu_set_ib_size(struct amdgpu_ib * ib)817 static void amdgpu_set_ib_size(struct amdgpu_ib *ib)
818 {
819 if (ib->ptr_ib_size_inside_ib) {
820 *ib->ptr_ib_size = ib->base.current.cdw |
821 S_3F2_CHAIN(1) | S_3F2_VALID(1);
822 } else {
823 *ib->ptr_ib_size = ib->base.current.cdw;
824 }
825 }
826
amdgpu_ib_finalize(struct amdgpu_winsys * ws,struct amdgpu_ib * ib)827 static void amdgpu_ib_finalize(struct amdgpu_winsys *ws, struct amdgpu_ib *ib)
828 {
829 amdgpu_set_ib_size(ib);
830 ib->used_ib_space += ib->base.current.cdw * 4;
831 ib->used_ib_space = align(ib->used_ib_space, ws->info.ib_alignment);
832 ib->max_ib_size = MAX2(ib->max_ib_size, ib->base.prev_dw + ib->base.current.cdw);
833 }
834
amdgpu_init_cs_context(struct amdgpu_winsys * ws,struct amdgpu_cs_context * cs,enum ring_type ring_type)835 static bool amdgpu_init_cs_context(struct amdgpu_winsys *ws,
836 struct amdgpu_cs_context *cs,
837 enum ring_type ring_type)
838 {
839 switch (ring_type) {
840 case RING_DMA:
841 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_DMA;
842 break;
843
844 case RING_UVD:
845 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_UVD;
846 break;
847
848 case RING_UVD_ENC:
849 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_UVD_ENC;
850 break;
851
852 case RING_VCE:
853 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCE;
854 break;
855
856 case RING_VCN_DEC:
857 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCN_DEC;
858 break;
859
860 case RING_VCN_ENC:
861 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCN_ENC;
862 break;
863
864 case RING_VCN_JPEG:
865 cs->ib[IB_MAIN].ip_type = AMDGPU_HW_IP_VCN_JPEG;
866 break;
867
868 case RING_COMPUTE:
869 case RING_GFX:
870 cs->ib[IB_MAIN].ip_type = ring_type == RING_GFX ? AMDGPU_HW_IP_GFX :
871 AMDGPU_HW_IP_COMPUTE;
872
873 /* The kernel shouldn't invalidate L2 and vL1. The proper place for cache
874 * invalidation is the beginning of IBs (the previous commit does that),
875 * because completion of an IB doesn't care about the state of GPU caches,
876 * but the beginning of an IB does. Draw calls from multiple IBs can be
877 * executed in parallel, so draw calls from the current IB can finish after
878 * the next IB starts drawing, and so the cache flush at the end of IB
879 * is always late.
880 */
881 if (ws->info.drm_minor >= 26)
882 cs->ib[IB_MAIN].flags = AMDGPU_IB_FLAG_TC_WB_NOT_INVALIDATE;
883 break;
884
885 default:
886 assert(0);
887 }
888
889 cs->ib[IB_PARALLEL_COMPUTE].ip_type = AMDGPU_HW_IP_COMPUTE;
890 cs->ib[IB_PARALLEL_COMPUTE].flags = AMDGPU_IB_FLAG_TC_WB_NOT_INVALIDATE;
891
892 memset(cs->buffer_indices_hashlist, -1, sizeof(cs->buffer_indices_hashlist));
893 cs->last_added_bo = NULL;
894 return true;
895 }
896
cleanup_fence_list(struct amdgpu_fence_list * fences)897 static void cleanup_fence_list(struct amdgpu_fence_list *fences)
898 {
899 for (unsigned i = 0; i < fences->num; i++)
900 amdgpu_fence_reference(&fences->list[i], NULL);
901 fences->num = 0;
902 }
903
amdgpu_cs_context_cleanup(struct amdgpu_cs_context * cs)904 static void amdgpu_cs_context_cleanup(struct amdgpu_cs_context *cs)
905 {
906 unsigned i;
907
908 for (i = 0; i < cs->num_real_buffers; i++) {
909 p_atomic_dec(&cs->real_buffers[i].bo->num_cs_references);
910 amdgpu_winsys_bo_reference(&cs->real_buffers[i].bo, NULL);
911 }
912 for (i = 0; i < cs->num_slab_buffers; i++) {
913 p_atomic_dec(&cs->slab_buffers[i].bo->num_cs_references);
914 amdgpu_winsys_bo_reference(&cs->slab_buffers[i].bo, NULL);
915 }
916 for (i = 0; i < cs->num_sparse_buffers; i++) {
917 p_atomic_dec(&cs->sparse_buffers[i].bo->num_cs_references);
918 amdgpu_winsys_bo_reference(&cs->sparse_buffers[i].bo, NULL);
919 }
920 cleanup_fence_list(&cs->fence_dependencies);
921 cleanup_fence_list(&cs->syncobj_dependencies);
922 cleanup_fence_list(&cs->syncobj_to_signal);
923 cleanup_fence_list(&cs->compute_fence_dependencies);
924 cleanup_fence_list(&cs->compute_start_fence_dependencies);
925
926 cs->num_real_buffers = 0;
927 cs->num_slab_buffers = 0;
928 cs->num_sparse_buffers = 0;
929 amdgpu_fence_reference(&cs->fence, NULL);
930
931 memset(cs->buffer_indices_hashlist, -1, sizeof(cs->buffer_indices_hashlist));
932 cs->last_added_bo = NULL;
933 }
934
amdgpu_destroy_cs_context(struct amdgpu_cs_context * cs)935 static void amdgpu_destroy_cs_context(struct amdgpu_cs_context *cs)
936 {
937 amdgpu_cs_context_cleanup(cs);
938 FREE(cs->real_buffers);
939 FREE(cs->slab_buffers);
940 FREE(cs->sparse_buffers);
941 FREE(cs->fence_dependencies.list);
942 FREE(cs->syncobj_dependencies.list);
943 FREE(cs->syncobj_to_signal.list);
944 FREE(cs->compute_fence_dependencies.list);
945 FREE(cs->compute_start_fence_dependencies.list);
946 }
947
948
949 static struct radeon_cmdbuf *
amdgpu_cs_create(struct radeon_winsys_ctx * rwctx,enum ring_type ring_type,void (* flush)(void * ctx,unsigned flags,struct pipe_fence_handle ** fence),void * flush_ctx,bool stop_exec_on_failure)950 amdgpu_cs_create(struct radeon_winsys_ctx *rwctx,
951 enum ring_type ring_type,
952 void (*flush)(void *ctx, unsigned flags,
953 struct pipe_fence_handle **fence),
954 void *flush_ctx,
955 bool stop_exec_on_failure)
956 {
957 struct amdgpu_ctx *ctx = (struct amdgpu_ctx*)rwctx;
958 struct amdgpu_cs *cs;
959
960 cs = CALLOC_STRUCT(amdgpu_cs);
961 if (!cs) {
962 return NULL;
963 }
964
965 util_queue_fence_init(&cs->flush_completed);
966
967 cs->ctx = ctx;
968 cs->flush_cs = flush;
969 cs->flush_data = flush_ctx;
970 cs->ring_type = ring_type;
971 cs->stop_exec_on_failure = stop_exec_on_failure;
972
973 struct amdgpu_cs_fence_info fence_info;
974 fence_info.handle = cs->ctx->user_fence_bo;
975 fence_info.offset = cs->ring_type * 4;
976 amdgpu_cs_chunk_fence_info_to_data(&fence_info, (void*)&cs->fence_chunk);
977
978 cs->main.ib_type = IB_MAIN;
979 cs->compute_ib.ib_type = IB_PARALLEL_COMPUTE;
980
981 if (!amdgpu_init_cs_context(ctx->ws, &cs->csc1, ring_type)) {
982 FREE(cs);
983 return NULL;
984 }
985
986 if (!amdgpu_init_cs_context(ctx->ws, &cs->csc2, ring_type)) {
987 amdgpu_destroy_cs_context(&cs->csc1);
988 FREE(cs);
989 return NULL;
990 }
991
992 /* Set the first submission context as current. */
993 cs->csc = &cs->csc1;
994 cs->cst = &cs->csc2;
995
996 if (!amdgpu_get_new_ib(ctx->ws, cs, IB_MAIN)) {
997 amdgpu_destroy_cs_context(&cs->csc2);
998 amdgpu_destroy_cs_context(&cs->csc1);
999 FREE(cs);
1000 return NULL;
1001 }
1002
1003 p_atomic_inc(&ctx->ws->num_cs);
1004 return &cs->main.base;
1005 }
1006
1007 static struct radeon_cmdbuf *
amdgpu_cs_add_parallel_compute_ib(struct radeon_cmdbuf * ib,bool uses_gds_ordered_append)1008 amdgpu_cs_add_parallel_compute_ib(struct radeon_cmdbuf *ib,
1009 bool uses_gds_ordered_append)
1010 {
1011 struct amdgpu_cs *cs = (struct amdgpu_cs*)ib;
1012 struct amdgpu_winsys *ws = cs->ctx->ws;
1013
1014 if (cs->ring_type != RING_GFX)
1015 return NULL;
1016
1017 /* only one secondary IB can be added */
1018 if (cs->compute_ib.ib_mapped)
1019 return NULL;
1020
1021 /* Allocate the compute IB. */
1022 if (!amdgpu_get_new_ib(ws, cs, IB_PARALLEL_COMPUTE))
1023 return NULL;
1024
1025 if (uses_gds_ordered_append) {
1026 cs->csc1.ib[IB_PARALLEL_COMPUTE].flags |=
1027 AMDGPU_IB_FLAG_RESET_GDS_MAX_WAVE_ID;
1028 cs->csc2.ib[IB_PARALLEL_COMPUTE].flags |=
1029 AMDGPU_IB_FLAG_RESET_GDS_MAX_WAVE_ID;
1030 }
1031 return &cs->compute_ib.base;
1032 }
1033
1034 static bool
amdgpu_cs_setup_preemption(struct radeon_cmdbuf * rcs,const uint32_t * preamble_ib,unsigned preamble_num_dw)1035 amdgpu_cs_setup_preemption(struct radeon_cmdbuf *rcs, const uint32_t *preamble_ib,
1036 unsigned preamble_num_dw)
1037 {
1038 struct amdgpu_ib *ib = amdgpu_ib(rcs);
1039 struct amdgpu_cs *cs = amdgpu_cs_from_ib(ib);
1040 struct amdgpu_winsys *ws = cs->ctx->ws;
1041 struct amdgpu_cs_context *csc[2] = {&cs->csc1, &cs->csc2};
1042 unsigned size = align(preamble_num_dw * 4, ws->info.ib_alignment);
1043 struct pb_buffer *preamble_bo;
1044 uint32_t *map;
1045
1046 /* Create the preamble IB buffer. */
1047 preamble_bo = amdgpu_bo_create(ws, size, ws->info.ib_alignment,
1048 RADEON_DOMAIN_VRAM,
1049 RADEON_FLAG_NO_INTERPROCESS_SHARING |
1050 RADEON_FLAG_GTT_WC |
1051 RADEON_FLAG_READ_ONLY);
1052 if (!preamble_bo)
1053 return false;
1054
1055 map = (uint32_t*)amdgpu_bo_map(preamble_bo, NULL,
1056 PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
1057 if (!map) {
1058 pb_reference(&preamble_bo, NULL);
1059 return false;
1060 }
1061
1062 /* Upload the preamble IB. */
1063 memcpy(map, preamble_ib, preamble_num_dw * 4);
1064
1065 /* Pad the IB. */
1066 uint32_t ib_pad_dw_mask = ws->info.ib_pad_dw_mask[cs->ring_type];
1067 while (preamble_num_dw & ib_pad_dw_mask)
1068 map[preamble_num_dw++] = PKT3_NOP_PAD;
1069 amdgpu_bo_unmap(preamble_bo);
1070
1071 for (unsigned i = 0; i < 2; i++) {
1072 csc[i]->ib[IB_PREAMBLE] = csc[i]->ib[IB_MAIN];
1073 csc[i]->ib[IB_PREAMBLE].flags |= AMDGPU_IB_FLAG_PREAMBLE;
1074 csc[i]->ib[IB_PREAMBLE].va_start = amdgpu_winsys_bo(preamble_bo)->va;
1075 csc[i]->ib[IB_PREAMBLE].ib_bytes = preamble_num_dw * 4;
1076
1077 csc[i]->ib[IB_MAIN].flags |= AMDGPU_IB_FLAG_PREEMPT;
1078 }
1079
1080 assert(!cs->preamble_ib_bo);
1081 cs->preamble_ib_bo = preamble_bo;
1082
1083 amdgpu_cs_add_buffer(rcs, cs->preamble_ib_bo, RADEON_USAGE_READ, 0,
1084 RADEON_PRIO_IB1);
1085 return true;
1086 }
1087
amdgpu_cs_validate(struct radeon_cmdbuf * rcs)1088 static bool amdgpu_cs_validate(struct radeon_cmdbuf *rcs)
1089 {
1090 return true;
1091 }
1092
amdgpu_cs_check_space(struct radeon_cmdbuf * rcs,unsigned dw,bool force_chaining)1093 static bool amdgpu_cs_check_space(struct radeon_cmdbuf *rcs, unsigned dw,
1094 bool force_chaining)
1095 {
1096 struct amdgpu_ib *ib = amdgpu_ib(rcs);
1097 struct amdgpu_cs *cs = amdgpu_cs_from_ib(ib);
1098 unsigned requested_size = rcs->prev_dw + rcs->current.cdw + dw;
1099 unsigned cs_epilog_dw = amdgpu_cs_epilog_dws(cs);
1100 unsigned need_byte_size = (dw + cs_epilog_dw) * 4;
1101 uint64_t va;
1102 uint32_t *new_ptr_ib_size;
1103
1104 assert(rcs->current.cdw <= rcs->current.max_dw);
1105
1106 /* 125% of the size for IB epilog. */
1107 unsigned safe_byte_size = need_byte_size + need_byte_size / 4;
1108 ib->max_check_space_size = MAX2(ib->max_check_space_size,
1109 safe_byte_size);
1110
1111 /* If force_chaining is true, we can't return. We have to chain. */
1112 if (!force_chaining) {
1113 if (requested_size > amdgpu_ib_max_submit_dwords(ib->ib_type))
1114 return false;
1115
1116 ib->max_ib_size = MAX2(ib->max_ib_size, requested_size);
1117
1118 if (rcs->current.max_dw - rcs->current.cdw >= dw)
1119 return true;
1120 }
1121
1122 if (!amdgpu_cs_has_chaining(cs)) {
1123 assert(!force_chaining);
1124 return false;
1125 }
1126
1127 /* Allocate a new chunk */
1128 if (rcs->num_prev >= rcs->max_prev) {
1129 unsigned new_max_prev = MAX2(1, 2 * rcs->max_prev);
1130 struct radeon_cmdbuf_chunk *new_prev;
1131
1132 new_prev = REALLOC(rcs->prev,
1133 sizeof(*new_prev) * rcs->max_prev,
1134 sizeof(*new_prev) * new_max_prev);
1135 if (!new_prev)
1136 return false;
1137
1138 rcs->prev = new_prev;
1139 rcs->max_prev = new_max_prev;
1140 }
1141
1142 if (!amdgpu_ib_new_buffer(cs->ctx->ws, ib, cs->ring_type))
1143 return false;
1144
1145 assert(ib->used_ib_space == 0);
1146 va = amdgpu_winsys_bo(ib->big_ib_buffer)->va;
1147
1148 /* This space was originally reserved. */
1149 rcs->current.max_dw += cs_epilog_dw;
1150
1151 /* Pad with NOPs but leave 4 dwords for INDIRECT_BUFFER. */
1152 uint32_t ib_pad_dw_mask = cs->ctx->ws->info.ib_pad_dw_mask[cs->ring_type];
1153 while ((rcs->current.cdw & ib_pad_dw_mask) != ib_pad_dw_mask - 3)
1154 radeon_emit(rcs, PKT3_NOP_PAD);
1155
1156 radeon_emit(rcs, PKT3(PKT3_INDIRECT_BUFFER_CIK, 2, 0));
1157 radeon_emit(rcs, va);
1158 radeon_emit(rcs, va >> 32);
1159 new_ptr_ib_size = &rcs->current.buf[rcs->current.cdw++];
1160 assert((rcs->current.cdw & ib_pad_dw_mask) == 0);
1161
1162 assert((rcs->current.cdw & 7) == 0);
1163 assert(rcs->current.cdw <= rcs->current.max_dw);
1164
1165 amdgpu_set_ib_size(ib);
1166 ib->ptr_ib_size = new_ptr_ib_size;
1167 ib->ptr_ib_size_inside_ib = true;
1168
1169 /* Hook up the new chunk */
1170 rcs->prev[rcs->num_prev].buf = rcs->current.buf;
1171 rcs->prev[rcs->num_prev].cdw = rcs->current.cdw;
1172 rcs->prev[rcs->num_prev].max_dw = rcs->current.cdw; /* no modifications */
1173 rcs->num_prev++;
1174
1175 ib->base.prev_dw += ib->base.current.cdw;
1176 ib->base.current.cdw = 0;
1177
1178 ib->base.current.buf = (uint32_t*)(ib->ib_mapped + ib->used_ib_space);
1179 ib->base.current.max_dw = ib->big_ib_buffer->size / 4 - cs_epilog_dw;
1180 ib->base.gpu_address = va;
1181
1182 amdgpu_cs_add_buffer(&cs->main.base, ib->big_ib_buffer,
1183 RADEON_USAGE_READ, 0, RADEON_PRIO_IB1);
1184
1185 return true;
1186 }
1187
amdgpu_cs_get_buffer_list(struct radeon_cmdbuf * rcs,struct radeon_bo_list_item * list)1188 static unsigned amdgpu_cs_get_buffer_list(struct radeon_cmdbuf *rcs,
1189 struct radeon_bo_list_item *list)
1190 {
1191 struct amdgpu_cs_context *cs = amdgpu_cs(rcs)->csc;
1192 int i;
1193
1194 if (list) {
1195 for (i = 0; i < cs->num_real_buffers; i++) {
1196 list[i].bo_size = cs->real_buffers[i].bo->base.size;
1197 list[i].vm_address = cs->real_buffers[i].bo->va;
1198 list[i].priority_usage = cs->real_buffers[i].u.real.priority_usage;
1199 }
1200 }
1201 return cs->num_real_buffers;
1202 }
1203
add_fence_to_list(struct amdgpu_fence_list * fences,struct amdgpu_fence * fence)1204 static void add_fence_to_list(struct amdgpu_fence_list *fences,
1205 struct amdgpu_fence *fence)
1206 {
1207 unsigned idx = fences->num++;
1208
1209 if (idx >= fences->max) {
1210 unsigned size;
1211 const unsigned increment = 8;
1212
1213 fences->max = idx + increment;
1214 size = fences->max * sizeof(fences->list[0]);
1215 fences->list = realloc(fences->list, size);
1216 /* Clear the newly-allocated elements. */
1217 memset(fences->list + idx, 0,
1218 increment * sizeof(fences->list[0]));
1219 }
1220 amdgpu_fence_reference(&fences->list[idx], (struct pipe_fence_handle*)fence);
1221 }
1222
is_noop_fence_dependency(struct amdgpu_cs * acs,struct amdgpu_fence * fence)1223 static bool is_noop_fence_dependency(struct amdgpu_cs *acs,
1224 struct amdgpu_fence *fence)
1225 {
1226 struct amdgpu_cs_context *cs = acs->csc;
1227
1228 /* Detect no-op dependencies only when there is only 1 ring,
1229 * because IBs on one ring are always executed one at a time.
1230 *
1231 * We always want no dependency between back-to-back gfx IBs, because
1232 * we need the parallelism between IBs for good performance.
1233 */
1234 if ((acs->ring_type == RING_GFX ||
1235 acs->ctx->ws->info.num_rings[acs->ring_type] == 1) &&
1236 !amdgpu_fence_is_syncobj(fence) &&
1237 fence->ctx == acs->ctx &&
1238 fence->fence.ip_type == cs->ib[IB_MAIN].ip_type &&
1239 fence->fence.ip_instance == cs->ib[IB_MAIN].ip_instance &&
1240 fence->fence.ring == cs->ib[IB_MAIN].ring)
1241 return true;
1242
1243 return amdgpu_fence_wait((void *)fence, 0, false);
1244 }
1245
amdgpu_cs_add_fence_dependency(struct radeon_cmdbuf * rws,struct pipe_fence_handle * pfence,unsigned dependency_flags)1246 static void amdgpu_cs_add_fence_dependency(struct radeon_cmdbuf *rws,
1247 struct pipe_fence_handle *pfence,
1248 unsigned dependency_flags)
1249 {
1250 struct amdgpu_cs *acs = amdgpu_cs(rws);
1251 struct amdgpu_cs_context *cs = acs->csc;
1252 struct amdgpu_fence *fence = (struct amdgpu_fence*)pfence;
1253
1254 util_queue_fence_wait(&fence->submitted);
1255
1256 if (dependency_flags & RADEON_DEPENDENCY_PARALLEL_COMPUTE_ONLY) {
1257 /* Syncobjs are not needed here. */
1258 assert(!amdgpu_fence_is_syncobj(fence));
1259
1260 if (acs->ctx->ws->info.has_scheduled_fence_dependency &&
1261 dependency_flags & RADEON_DEPENDENCY_START_FENCE)
1262 add_fence_to_list(&cs->compute_start_fence_dependencies, fence);
1263 else
1264 add_fence_to_list(&cs->compute_fence_dependencies, fence);
1265 return;
1266 }
1267
1268 /* Start fences are not needed here. */
1269 assert(!(dependency_flags & RADEON_DEPENDENCY_START_FENCE));
1270
1271 if (is_noop_fence_dependency(acs, fence))
1272 return;
1273
1274 if (amdgpu_fence_is_syncobj(fence))
1275 add_fence_to_list(&cs->syncobj_dependencies, fence);
1276 else
1277 add_fence_to_list(&cs->fence_dependencies, fence);
1278 }
1279
amdgpu_add_bo_fence_dependencies(struct amdgpu_cs * acs,struct amdgpu_cs_buffer * buffer)1280 static void amdgpu_add_bo_fence_dependencies(struct amdgpu_cs *acs,
1281 struct amdgpu_cs_buffer *buffer)
1282 {
1283 struct amdgpu_cs_context *cs = acs->csc;
1284 struct amdgpu_winsys_bo *bo = buffer->bo;
1285 unsigned new_num_fences = 0;
1286
1287 for (unsigned j = 0; j < bo->num_fences; ++j) {
1288 struct amdgpu_fence *bo_fence = (void *)bo->fences[j];
1289
1290 if (is_noop_fence_dependency(acs, bo_fence))
1291 continue;
1292
1293 amdgpu_fence_reference(&bo->fences[new_num_fences], bo->fences[j]);
1294 new_num_fences++;
1295
1296 if (!(buffer->usage & RADEON_USAGE_SYNCHRONIZED))
1297 continue;
1298
1299 add_fence_to_list(&cs->fence_dependencies, bo_fence);
1300 }
1301
1302 for (unsigned j = new_num_fences; j < bo->num_fences; ++j)
1303 amdgpu_fence_reference(&bo->fences[j], NULL);
1304
1305 bo->num_fences = new_num_fences;
1306 }
1307
1308 /* Add the given list of fences to the buffer's fence list.
1309 *
1310 * Must be called with the winsys bo_fence_lock held.
1311 */
amdgpu_add_fences(struct amdgpu_winsys_bo * bo,unsigned num_fences,struct pipe_fence_handle ** fences)1312 void amdgpu_add_fences(struct amdgpu_winsys_bo *bo,
1313 unsigned num_fences,
1314 struct pipe_fence_handle **fences)
1315 {
1316 if (bo->num_fences + num_fences > bo->max_fences) {
1317 unsigned new_max_fences = MAX2(bo->num_fences + num_fences, bo->max_fences * 2);
1318 struct pipe_fence_handle **new_fences =
1319 REALLOC(bo->fences,
1320 bo->num_fences * sizeof(*new_fences),
1321 new_max_fences * sizeof(*new_fences));
1322 if (likely(new_fences)) {
1323 bo->fences = new_fences;
1324 bo->max_fences = new_max_fences;
1325 } else {
1326 unsigned drop;
1327
1328 fprintf(stderr, "amdgpu_add_fences: allocation failure, dropping fence(s)\n");
1329 if (!bo->num_fences)
1330 return;
1331
1332 bo->num_fences--; /* prefer to keep the most recent fence if possible */
1333 amdgpu_fence_reference(&bo->fences[bo->num_fences], NULL);
1334
1335 drop = bo->num_fences + num_fences - bo->max_fences;
1336 num_fences -= drop;
1337 fences += drop;
1338 }
1339 }
1340
1341 for (unsigned i = 0; i < num_fences; ++i) {
1342 bo->fences[bo->num_fences] = NULL;
1343 amdgpu_fence_reference(&bo->fences[bo->num_fences], fences[i]);
1344 bo->num_fences++;
1345 }
1346 }
1347
amdgpu_add_fence_dependencies_bo_list(struct amdgpu_cs * acs,struct pipe_fence_handle * fence,unsigned num_buffers,struct amdgpu_cs_buffer * buffers)1348 static void amdgpu_add_fence_dependencies_bo_list(struct amdgpu_cs *acs,
1349 struct pipe_fence_handle *fence,
1350 unsigned num_buffers,
1351 struct amdgpu_cs_buffer *buffers)
1352 {
1353 for (unsigned i = 0; i < num_buffers; i++) {
1354 struct amdgpu_cs_buffer *buffer = &buffers[i];
1355 struct amdgpu_winsys_bo *bo = buffer->bo;
1356
1357 amdgpu_add_bo_fence_dependencies(acs, buffer);
1358 p_atomic_inc(&bo->num_active_ioctls);
1359 amdgpu_add_fences(bo, 1, &fence);
1360 }
1361 }
1362
1363 /* Since the kernel driver doesn't synchronize execution between different
1364 * rings automatically, we have to add fence dependencies manually.
1365 */
amdgpu_add_fence_dependencies_bo_lists(struct amdgpu_cs * acs)1366 static void amdgpu_add_fence_dependencies_bo_lists(struct amdgpu_cs *acs)
1367 {
1368 struct amdgpu_cs_context *cs = acs->csc;
1369
1370 amdgpu_add_fence_dependencies_bo_list(acs, cs->fence, cs->num_real_buffers, cs->real_buffers);
1371 amdgpu_add_fence_dependencies_bo_list(acs, cs->fence, cs->num_slab_buffers, cs->slab_buffers);
1372 amdgpu_add_fence_dependencies_bo_list(acs, cs->fence, cs->num_sparse_buffers, cs->sparse_buffers);
1373 }
1374
amdgpu_cs_add_syncobj_signal(struct radeon_cmdbuf * rws,struct pipe_fence_handle * fence)1375 static void amdgpu_cs_add_syncobj_signal(struct radeon_cmdbuf *rws,
1376 struct pipe_fence_handle *fence)
1377 {
1378 struct amdgpu_cs *acs = amdgpu_cs(rws);
1379 struct amdgpu_cs_context *cs = acs->csc;
1380
1381 assert(amdgpu_fence_is_syncobj((struct amdgpu_fence *)fence));
1382
1383 add_fence_to_list(&cs->syncobj_to_signal, (struct amdgpu_fence*)fence);
1384 }
1385
1386 /* Add backing of sparse buffers to the buffer list.
1387 *
1388 * This is done late, during submission, to keep the buffer list short before
1389 * submit, and to avoid managing fences for the backing buffers.
1390 */
amdgpu_add_sparse_backing_buffers(struct amdgpu_cs_context * cs)1391 static bool amdgpu_add_sparse_backing_buffers(struct amdgpu_cs_context *cs)
1392 {
1393 for (unsigned i = 0; i < cs->num_sparse_buffers; ++i) {
1394 struct amdgpu_cs_buffer *buffer = &cs->sparse_buffers[i];
1395 struct amdgpu_winsys_bo *bo = buffer->bo;
1396
1397 simple_mtx_lock(&bo->lock);
1398
1399 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
1400 /* We can directly add the buffer here, because we know that each
1401 * backing buffer occurs only once.
1402 */
1403 int idx = amdgpu_do_add_real_buffer(cs, backing->bo);
1404 if (idx < 0) {
1405 fprintf(stderr, "%s: failed to add buffer\n", __FUNCTION__);
1406 simple_mtx_unlock(&bo->lock);
1407 return false;
1408 }
1409
1410 cs->real_buffers[idx].usage = buffer->usage & ~RADEON_USAGE_SYNCHRONIZED;
1411 cs->real_buffers[idx].u.real.priority_usage = buffer->u.real.priority_usage;
1412 p_atomic_inc(&backing->bo->num_active_ioctls);
1413 }
1414
1415 simple_mtx_unlock(&bo->lock);
1416 }
1417
1418 return true;
1419 }
1420
amdgpu_cs_submit_ib(void * job,int thread_index)1421 static void amdgpu_cs_submit_ib(void *job, int thread_index)
1422 {
1423 struct amdgpu_cs *acs = (struct amdgpu_cs*)job;
1424 struct amdgpu_winsys *ws = acs->ctx->ws;
1425 struct amdgpu_cs_context *cs = acs->cst;
1426 int i, r;
1427 uint32_t bo_list = 0;
1428 uint64_t seq_no = 0;
1429 bool has_user_fence = amdgpu_cs_has_user_fence(cs);
1430 bool use_bo_list_create = ws->info.drm_minor < 27;
1431 struct drm_amdgpu_bo_list_in bo_list_in;
1432
1433 /* Prepare the buffer list. */
1434 if (ws->debug_all_bos) {
1435 /* The buffer list contains all buffers. This is a slow path that
1436 * ensures that no buffer is missing in the BO list.
1437 */
1438 unsigned num_handles = 0;
1439 struct drm_amdgpu_bo_list_entry *list =
1440 alloca(ws->num_buffers * sizeof(struct drm_amdgpu_bo_list_entry));
1441 struct amdgpu_winsys_bo *bo;
1442
1443 simple_mtx_lock(&ws->global_bo_list_lock);
1444 LIST_FOR_EACH_ENTRY(bo, &ws->global_bo_list, u.real.global_list_item) {
1445 list[num_handles].bo_handle = bo->u.real.kms_handle;
1446 list[num_handles].bo_priority = 0;
1447 ++num_handles;
1448 }
1449
1450 r = amdgpu_bo_list_create_raw(ws->dev, ws->num_buffers, list, &bo_list);
1451 simple_mtx_unlock(&ws->global_bo_list_lock);
1452 if (r) {
1453 fprintf(stderr, "amdgpu: buffer list creation failed (%d)\n", r);
1454 goto cleanup;
1455 }
1456 } else {
1457 if (!amdgpu_add_sparse_backing_buffers(cs)) {
1458 fprintf(stderr, "amdgpu: amdgpu_add_sparse_backing_buffers failed\n");
1459 r = -ENOMEM;
1460 goto cleanup;
1461 }
1462
1463 struct drm_amdgpu_bo_list_entry *list =
1464 alloca((cs->num_real_buffers + 2) * sizeof(struct drm_amdgpu_bo_list_entry));
1465
1466 unsigned num_handles = 0;
1467 for (i = 0; i < cs->num_real_buffers; ++i) {
1468 struct amdgpu_cs_buffer *buffer = &cs->real_buffers[i];
1469 assert(buffer->u.real.priority_usage != 0);
1470
1471 list[num_handles].bo_handle = buffer->bo->u.real.kms_handle;
1472 list[num_handles].bo_priority = (util_last_bit(buffer->u.real.priority_usage) - 1) / 2;
1473 ++num_handles;
1474 }
1475
1476 if (use_bo_list_create) {
1477 /* Legacy path creating the buffer list handle and passing it to the CS ioctl. */
1478 r = amdgpu_bo_list_create_raw(ws->dev, num_handles, list, &bo_list);
1479 if (r) {
1480 fprintf(stderr, "amdgpu: buffer list creation failed (%d)\n", r);
1481 goto cleanup;
1482 }
1483 } else {
1484 /* Standard path passing the buffer list via the CS ioctl. */
1485 bo_list_in.operation = ~0;
1486 bo_list_in.list_handle = ~0;
1487 bo_list_in.bo_number = num_handles;
1488 bo_list_in.bo_info_size = sizeof(struct drm_amdgpu_bo_list_entry);
1489 bo_list_in.bo_info_ptr = (uint64_t)(uintptr_t)list;
1490 }
1491 }
1492
1493 if (acs->ring_type == RING_GFX)
1494 ws->gfx_bo_list_counter += cs->num_real_buffers;
1495
1496 if (acs->stop_exec_on_failure && acs->ctx->num_rejected_cs) {
1497 r = -ECANCELED;
1498 } else {
1499 struct drm_amdgpu_cs_chunk chunks[7];
1500 unsigned num_chunks = 0;
1501
1502 /* BO list */
1503 if (!use_bo_list_create) {
1504 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_BO_HANDLES;
1505 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_bo_list_in) / 4;
1506 chunks[num_chunks].chunk_data = (uintptr_t)&bo_list_in;
1507 num_chunks++;
1508 }
1509
1510 /* Fence dependencies. */
1511 unsigned num_dependencies = cs->fence_dependencies.num;
1512 if (num_dependencies) {
1513 struct drm_amdgpu_cs_chunk_dep *dep_chunk =
1514 alloca(num_dependencies * sizeof(*dep_chunk));
1515
1516 for (unsigned i = 0; i < num_dependencies; i++) {
1517 struct amdgpu_fence *fence =
1518 (struct amdgpu_fence*)cs->fence_dependencies.list[i];
1519
1520 assert(util_queue_fence_is_signalled(&fence->submitted));
1521 amdgpu_cs_chunk_fence_to_dep(&fence->fence, &dep_chunk[i]);
1522 }
1523
1524 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_DEPENDENCIES;
1525 chunks[num_chunks].length_dw = sizeof(dep_chunk[0]) / 4 * num_dependencies;
1526 chunks[num_chunks].chunk_data = (uintptr_t)dep_chunk;
1527 num_chunks++;
1528 }
1529
1530 /* Syncobj dependencies. */
1531 unsigned num_syncobj_dependencies = cs->syncobj_dependencies.num;
1532 if (num_syncobj_dependencies) {
1533 struct drm_amdgpu_cs_chunk_sem *sem_chunk =
1534 alloca(num_syncobj_dependencies * sizeof(sem_chunk[0]));
1535
1536 for (unsigned i = 0; i < num_syncobj_dependencies; i++) {
1537 struct amdgpu_fence *fence =
1538 (struct amdgpu_fence*)cs->syncobj_dependencies.list[i];
1539
1540 if (!amdgpu_fence_is_syncobj(fence))
1541 continue;
1542
1543 assert(util_queue_fence_is_signalled(&fence->submitted));
1544 sem_chunk[i].handle = fence->syncobj;
1545 }
1546
1547 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_SYNCOBJ_IN;
1548 chunks[num_chunks].length_dw = sizeof(sem_chunk[0]) / 4 * num_syncobj_dependencies;
1549 chunks[num_chunks].chunk_data = (uintptr_t)sem_chunk;
1550 num_chunks++;
1551 }
1552
1553 /* Submit the parallel compute IB first. */
1554 if (cs->ib[IB_PARALLEL_COMPUTE].ib_bytes > 0) {
1555 unsigned old_num_chunks = num_chunks;
1556
1557 /* Add compute fence dependencies. */
1558 unsigned num_dependencies = cs->compute_fence_dependencies.num;
1559 if (num_dependencies) {
1560 struct drm_amdgpu_cs_chunk_dep *dep_chunk =
1561 alloca(num_dependencies * sizeof(*dep_chunk));
1562
1563 for (unsigned i = 0; i < num_dependencies; i++) {
1564 struct amdgpu_fence *fence =
1565 (struct amdgpu_fence*)cs->compute_fence_dependencies.list[i];
1566
1567 assert(util_queue_fence_is_signalled(&fence->submitted));
1568 amdgpu_cs_chunk_fence_to_dep(&fence->fence, &dep_chunk[i]);
1569 }
1570
1571 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_DEPENDENCIES;
1572 chunks[num_chunks].length_dw = sizeof(dep_chunk[0]) / 4 * num_dependencies;
1573 chunks[num_chunks].chunk_data = (uintptr_t)dep_chunk;
1574 num_chunks++;
1575 }
1576
1577 /* Add compute start fence dependencies. */
1578 unsigned num_start_dependencies = cs->compute_start_fence_dependencies.num;
1579 if (num_start_dependencies) {
1580 struct drm_amdgpu_cs_chunk_dep *dep_chunk =
1581 alloca(num_start_dependencies * sizeof(*dep_chunk));
1582
1583 for (unsigned i = 0; i < num_start_dependencies; i++) {
1584 struct amdgpu_fence *fence =
1585 (struct amdgpu_fence*)cs->compute_start_fence_dependencies.list[i];
1586
1587 assert(util_queue_fence_is_signalled(&fence->submitted));
1588 amdgpu_cs_chunk_fence_to_dep(&fence->fence, &dep_chunk[i]);
1589 }
1590
1591 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES;
1592 chunks[num_chunks].length_dw = sizeof(dep_chunk[0]) / 4 * num_start_dependencies;
1593 chunks[num_chunks].chunk_data = (uintptr_t)dep_chunk;
1594 num_chunks++;
1595 }
1596
1597 /* Convert from dwords to bytes. */
1598 cs->ib[IB_PARALLEL_COMPUTE].ib_bytes *= 4;
1599 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_IB;
1600 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_cs_chunk_ib) / 4;
1601 chunks[num_chunks].chunk_data = (uintptr_t)&cs->ib[IB_PARALLEL_COMPUTE];
1602 num_chunks++;
1603
1604 r = amdgpu_cs_submit_raw2(ws->dev, acs->ctx->ctx, bo_list,
1605 num_chunks, chunks, NULL);
1606 if (r)
1607 goto finalize;
1608
1609 /* Back off the compute chunks. */
1610 num_chunks = old_num_chunks;
1611 }
1612
1613 /* Syncobj signals. */
1614 unsigned num_syncobj_to_signal = cs->syncobj_to_signal.num;
1615 if (num_syncobj_to_signal) {
1616 struct drm_amdgpu_cs_chunk_sem *sem_chunk =
1617 alloca(num_syncobj_to_signal * sizeof(sem_chunk[0]));
1618
1619 for (unsigned i = 0; i < num_syncobj_to_signal; i++) {
1620 struct amdgpu_fence *fence =
1621 (struct amdgpu_fence*)cs->syncobj_to_signal.list[i];
1622
1623 assert(amdgpu_fence_is_syncobj(fence));
1624 sem_chunk[i].handle = fence->syncobj;
1625 }
1626
1627 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_SYNCOBJ_OUT;
1628 chunks[num_chunks].length_dw = sizeof(sem_chunk[0]) / 4
1629 * num_syncobj_to_signal;
1630 chunks[num_chunks].chunk_data = (uintptr_t)sem_chunk;
1631 num_chunks++;
1632 }
1633
1634 /* Fence */
1635 if (has_user_fence) {
1636 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_FENCE;
1637 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_cs_chunk_fence) / 4;
1638 chunks[num_chunks].chunk_data = (uintptr_t)&acs->fence_chunk;
1639 num_chunks++;
1640 }
1641
1642 /* IB */
1643 if (cs->ib[IB_PREAMBLE].ib_bytes) {
1644 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_IB;
1645 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_cs_chunk_ib) / 4;
1646 chunks[num_chunks].chunk_data = (uintptr_t)&cs->ib[IB_PREAMBLE];
1647 num_chunks++;
1648 }
1649
1650 /* IB */
1651 cs->ib[IB_MAIN].ib_bytes *= 4; /* Convert from dwords to bytes. */
1652 chunks[num_chunks].chunk_id = AMDGPU_CHUNK_ID_IB;
1653 chunks[num_chunks].length_dw = sizeof(struct drm_amdgpu_cs_chunk_ib) / 4;
1654 chunks[num_chunks].chunk_data = (uintptr_t)&cs->ib[IB_MAIN];
1655 num_chunks++;
1656
1657 if (cs->secure) {
1658 cs->ib[IB_PREAMBLE].flags |= AMDGPU_IB_FLAGS_SECURE;
1659 cs->ib[IB_MAIN].flags |= AMDGPU_IB_FLAGS_SECURE;
1660 } else {
1661 cs->ib[IB_PREAMBLE].flags &= ~AMDGPU_IB_FLAGS_SECURE;
1662 cs->ib[IB_MAIN].flags &= ~AMDGPU_IB_FLAGS_SECURE;
1663 }
1664
1665 assert(num_chunks <= ARRAY_SIZE(chunks));
1666
1667 r = amdgpu_cs_submit_raw2(ws->dev, acs->ctx->ctx, bo_list,
1668 num_chunks, chunks, &seq_no);
1669 }
1670 finalize:
1671
1672 if (r) {
1673 if (r == -ENOMEM)
1674 fprintf(stderr, "amdgpu: Not enough memory for command submission.\n");
1675 else if (r == -ECANCELED)
1676 fprintf(stderr, "amdgpu: The CS has been cancelled because the context is lost.\n");
1677 else
1678 fprintf(stderr, "amdgpu: The CS has been rejected, "
1679 "see dmesg for more information (%i).\n", r);
1680
1681 acs->ctx->num_rejected_cs++;
1682 ws->num_total_rejected_cs++;
1683 } else {
1684 /* Success. */
1685 uint64_t *user_fence = NULL;
1686
1687 /* Need to reserve 4 QWORD for user fence:
1688 * QWORD[0]: completed fence
1689 * QWORD[1]: preempted fence
1690 * QWORD[2]: reset fence
1691 * QWORD[3]: preempted then reset
1692 **/
1693 if (has_user_fence)
1694 user_fence = acs->ctx->user_fence_cpu_address_base + acs->ring_type * 4;
1695 amdgpu_fence_submitted(cs->fence, seq_no, user_fence);
1696 }
1697
1698 /* Cleanup. */
1699 if (bo_list)
1700 amdgpu_bo_list_destroy_raw(ws->dev, bo_list);
1701
1702 cleanup:
1703 /* If there was an error, signal the fence, because it won't be signalled
1704 * by the hardware. */
1705 if (r)
1706 amdgpu_fence_signalled(cs->fence);
1707
1708 cs->error_code = r;
1709
1710 for (i = 0; i < cs->num_real_buffers; i++)
1711 p_atomic_dec(&cs->real_buffers[i].bo->num_active_ioctls);
1712 for (i = 0; i < cs->num_slab_buffers; i++)
1713 p_atomic_dec(&cs->slab_buffers[i].bo->num_active_ioctls);
1714 for (i = 0; i < cs->num_sparse_buffers; i++)
1715 p_atomic_dec(&cs->sparse_buffers[i].bo->num_active_ioctls);
1716
1717 amdgpu_cs_context_cleanup(cs);
1718 }
1719
1720 /* Make sure the previous submission is completed. */
amdgpu_cs_sync_flush(struct radeon_cmdbuf * rcs)1721 void amdgpu_cs_sync_flush(struct radeon_cmdbuf *rcs)
1722 {
1723 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1724
1725 /* Wait for any pending ioctl of this CS to complete. */
1726 util_queue_fence_wait(&cs->flush_completed);
1727 }
1728
amdgpu_cs_flush(struct radeon_cmdbuf * rcs,unsigned flags,struct pipe_fence_handle ** fence)1729 static int amdgpu_cs_flush(struct radeon_cmdbuf *rcs,
1730 unsigned flags,
1731 struct pipe_fence_handle **fence)
1732 {
1733 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1734 struct amdgpu_winsys *ws = cs->ctx->ws;
1735 int error_code = 0;
1736 uint32_t ib_pad_dw_mask = ws->info.ib_pad_dw_mask[cs->ring_type];
1737
1738 rcs->current.max_dw += amdgpu_cs_epilog_dws(cs);
1739
1740 /* Pad the IB according to the mask. */
1741 switch (cs->ring_type) {
1742 case RING_DMA:
1743 if (ws->info.chip_class <= GFX6) {
1744 while (rcs->current.cdw & ib_pad_dw_mask)
1745 radeon_emit(rcs, 0xf0000000); /* NOP packet */
1746 } else {
1747 while (rcs->current.cdw & ib_pad_dw_mask)
1748 radeon_emit(rcs, 0x00000000); /* NOP packet */
1749 }
1750 break;
1751 case RING_GFX:
1752 case RING_COMPUTE:
1753 if (ws->info.gfx_ib_pad_with_type2) {
1754 while (rcs->current.cdw & ib_pad_dw_mask)
1755 radeon_emit(rcs, PKT2_NOP_PAD);
1756 } else {
1757 while (rcs->current.cdw & ib_pad_dw_mask)
1758 radeon_emit(rcs, PKT3_NOP_PAD);
1759 }
1760 if (cs->ring_type == RING_GFX)
1761 ws->gfx_ib_size_counter += (rcs->prev_dw + rcs->current.cdw) * 4;
1762
1763 /* Also pad secondary IBs. */
1764 if (cs->compute_ib.ib_mapped) {
1765 while (cs->compute_ib.base.current.cdw & ib_pad_dw_mask)
1766 radeon_emit(&cs->compute_ib.base, PKT3_NOP_PAD);
1767 }
1768 break;
1769 case RING_UVD:
1770 case RING_UVD_ENC:
1771 while (rcs->current.cdw & ib_pad_dw_mask)
1772 radeon_emit(rcs, 0x80000000); /* type2 nop packet */
1773 break;
1774 case RING_VCN_JPEG:
1775 if (rcs->current.cdw % 2)
1776 assert(0);
1777 while (rcs->current.cdw & ib_pad_dw_mask) {
1778 radeon_emit(rcs, 0x60000000); /* nop packet */
1779 radeon_emit(rcs, 0x00000000);
1780 }
1781 break;
1782 case RING_VCN_DEC:
1783 while (rcs->current.cdw & ib_pad_dw_mask)
1784 radeon_emit(rcs, 0x81ff); /* nop packet */
1785 break;
1786 default:
1787 break;
1788 }
1789
1790 if (rcs->current.cdw > rcs->current.max_dw) {
1791 fprintf(stderr, "amdgpu: command stream overflowed\n");
1792 }
1793
1794 /* If the CS is not empty or overflowed.... */
1795 if (likely(radeon_emitted(&cs->main.base, 0) &&
1796 cs->main.base.current.cdw <= cs->main.base.current.max_dw &&
1797 !debug_get_option_noop() &&
1798 !(flags & RADEON_FLUSH_NOOP))) {
1799 struct amdgpu_cs_context *cur = cs->csc;
1800
1801 /* Set IB sizes. */
1802 amdgpu_ib_finalize(ws, &cs->main);
1803
1804 if (cs->compute_ib.ib_mapped)
1805 amdgpu_ib_finalize(ws, &cs->compute_ib);
1806
1807 /* Create a fence. */
1808 amdgpu_fence_reference(&cur->fence, NULL);
1809 if (cs->next_fence) {
1810 /* just move the reference */
1811 cur->fence = cs->next_fence;
1812 cs->next_fence = NULL;
1813 } else {
1814 cur->fence = amdgpu_fence_create(cs->ctx,
1815 cur->ib[IB_MAIN].ip_type,
1816 cur->ib[IB_MAIN].ip_instance,
1817 cur->ib[IB_MAIN].ring);
1818 }
1819 if (fence)
1820 amdgpu_fence_reference(fence, cur->fence);
1821
1822 amdgpu_cs_sync_flush(rcs);
1823
1824 /* Prepare buffers.
1825 *
1826 * This fence must be held until the submission is queued to ensure
1827 * that the order of fence dependency updates matches the order of
1828 * submissions.
1829 */
1830 simple_mtx_lock(&ws->bo_fence_lock);
1831 amdgpu_add_fence_dependencies_bo_lists(cs);
1832
1833 /* Swap command streams. "cst" is going to be submitted. */
1834 cs->csc = cs->cst;
1835 cs->cst = cur;
1836
1837 /* Submit. */
1838 util_queue_add_job(&ws->cs_queue, cs, &cs->flush_completed,
1839 amdgpu_cs_submit_ib, NULL, 0);
1840
1841 if (flags & RADEON_FLUSH_TOGGLE_SECURE_SUBMISSION)
1842 cs->csc->secure = !cs->cst->secure;
1843 else
1844 cs->csc->secure = cs->cst->secure;
1845
1846 /* The submission has been queued, unlock the fence now. */
1847 simple_mtx_unlock(&ws->bo_fence_lock);
1848
1849 if (!(flags & PIPE_FLUSH_ASYNC)) {
1850 amdgpu_cs_sync_flush(rcs);
1851 error_code = cur->error_code;
1852 }
1853 } else {
1854 if (flags & RADEON_FLUSH_TOGGLE_SECURE_SUBMISSION)
1855 cs->csc->secure = !cs->csc->secure;
1856 amdgpu_cs_context_cleanup(cs->csc);
1857 }
1858
1859 amdgpu_get_new_ib(ws, cs, IB_MAIN);
1860 if (cs->compute_ib.ib_mapped)
1861 amdgpu_get_new_ib(ws, cs, IB_PARALLEL_COMPUTE);
1862
1863 if (cs->preamble_ib_bo) {
1864 amdgpu_cs_add_buffer(rcs, cs->preamble_ib_bo, RADEON_USAGE_READ, 0,
1865 RADEON_PRIO_IB1);
1866 }
1867
1868 cs->main.base.used_gart = 0;
1869 cs->main.base.used_vram = 0;
1870
1871 if (cs->ring_type == RING_GFX)
1872 ws->num_gfx_IBs++;
1873 else if (cs->ring_type == RING_DMA)
1874 ws->num_sdma_IBs++;
1875
1876 return error_code;
1877 }
1878
amdgpu_cs_destroy(struct radeon_cmdbuf * rcs)1879 static void amdgpu_cs_destroy(struct radeon_cmdbuf *rcs)
1880 {
1881 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1882
1883 amdgpu_cs_sync_flush(rcs);
1884 util_queue_fence_destroy(&cs->flush_completed);
1885 p_atomic_dec(&cs->ctx->ws->num_cs);
1886 pb_reference(&cs->preamble_ib_bo, NULL);
1887 pb_reference(&cs->main.big_ib_buffer, NULL);
1888 FREE(cs->main.base.prev);
1889 pb_reference(&cs->compute_ib.big_ib_buffer, NULL);
1890 FREE(cs->compute_ib.base.prev);
1891 amdgpu_destroy_cs_context(&cs->csc1);
1892 amdgpu_destroy_cs_context(&cs->csc2);
1893 amdgpu_fence_reference(&cs->next_fence, NULL);
1894 FREE(cs);
1895 }
1896
amdgpu_bo_is_referenced(struct radeon_cmdbuf * rcs,struct pb_buffer * _buf,enum radeon_bo_usage usage)1897 static bool amdgpu_bo_is_referenced(struct radeon_cmdbuf *rcs,
1898 struct pb_buffer *_buf,
1899 enum radeon_bo_usage usage)
1900 {
1901 struct amdgpu_cs *cs = amdgpu_cs(rcs);
1902 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)_buf;
1903
1904 return amdgpu_bo_is_referenced_by_cs_with_usage(cs, bo, usage);
1905 }
1906
amdgpu_cs_init_functions(struct amdgpu_screen_winsys * ws)1907 void amdgpu_cs_init_functions(struct amdgpu_screen_winsys *ws)
1908 {
1909 ws->base.ctx_create = amdgpu_ctx_create;
1910 ws->base.ctx_destroy = amdgpu_ctx_destroy;
1911 ws->base.ctx_query_reset_status = amdgpu_ctx_query_reset_status;
1912 ws->base.cs_create = amdgpu_cs_create;
1913 ws->base.cs_add_parallel_compute_ib = amdgpu_cs_add_parallel_compute_ib;
1914 ws->base.cs_setup_preemption = amdgpu_cs_setup_preemption;
1915 ws->base.cs_destroy = amdgpu_cs_destroy;
1916 ws->base.cs_add_buffer = amdgpu_cs_add_buffer;
1917 ws->base.cs_validate = amdgpu_cs_validate;
1918 ws->base.cs_check_space = amdgpu_cs_check_space;
1919 ws->base.cs_get_buffer_list = amdgpu_cs_get_buffer_list;
1920 ws->base.cs_flush = amdgpu_cs_flush;
1921 ws->base.cs_get_next_fence = amdgpu_cs_get_next_fence;
1922 ws->base.cs_is_buffer_referenced = amdgpu_bo_is_referenced;
1923 ws->base.cs_sync_flush = amdgpu_cs_sync_flush;
1924 ws->base.cs_add_fence_dependency = amdgpu_cs_add_fence_dependency;
1925 ws->base.cs_add_syncobj_signal = amdgpu_cs_add_syncobj_signal;
1926 ws->base.fence_wait = amdgpu_fence_wait_rel_timeout;
1927 ws->base.fence_reference = amdgpu_fence_reference;
1928 ws->base.fence_import_syncobj = amdgpu_fence_import_syncobj;
1929 ws->base.fence_import_sync_file = amdgpu_fence_import_sync_file;
1930 ws->base.fence_export_sync_file = amdgpu_fence_export_sync_file;
1931 ws->base.export_signalled_sync_file = amdgpu_export_signalled_sync_file;
1932 }
1933