summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/radeonsi/si_fence.c
blob: 3d23597413c6f17683f0dbb417e71b6414fb3752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*
 * Copyright 2013-2017 Advanced Micro Devices, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

#include <libsync.h>

#include "util/os_time.h"
#include "util/u_memory.h"
#include "util/u_queue.h"
#include "util/u_upload_mgr.h"

#include "si_build_pm4.h"

struct si_fine_fence {
	struct si_resource *buf;
	unsigned offset;
};

struct si_multi_fence {
	struct pipe_reference reference;
	struct pipe_fence_handle *gfx;
	struct pipe_fence_handle *sdma;
	struct tc_unflushed_batch_token *tc_token;
	struct util_queue_fence ready;

	/* If the context wasn't flushed at fence creation, this is non-NULL. */
	struct {
		struct si_context *ctx;
		unsigned ib_index;
	} gfx_unflushed;

	struct si_fine_fence fine;
};

/**
 * Write an EOP event.
 *
 * \param event		EVENT_TYPE_*
 * \param event_flags	Optional cache flush flags (TC)
 * \param dst_sel       MEM or TC_L2
 * \param int_sel       NONE or SEND_DATA_AFTER_WR_CONFIRM
 * \param data_sel	DISCARD, VALUE_32BIT, TIMESTAMP, or GDS
 * \param buf		Buffer
 * \param va		GPU address
 * \param old_value	Previous fence value (for a bug workaround)
 * \param new_value	Fence value to write for this event.
 */
void si_cp_release_mem(struct si_context *ctx,
		       unsigned event, unsigned event_flags,
		       unsigned dst_sel, unsigned int_sel, unsigned data_sel,
		       struct si_resource *buf, uint64_t va,
		       uint32_t new_fence, unsigned query_type)
{
	struct radeon_cmdbuf *cs = ctx->gfx_cs;
	unsigned op = EVENT_TYPE(event) |
		      EVENT_INDEX(event == V_028A90_CS_DONE ||
				  event == V_028A90_PS_DONE ? 6 : 5) |
		      event_flags;
	unsigned sel = EOP_DST_SEL(dst_sel) |
		       EOP_INT_SEL(int_sel) |
		       EOP_DATA_SEL(data_sel);

	if (ctx->chip_class >= GFX9) {
		/* A ZPASS_DONE or PIXEL_STAT_DUMP_EVENT (of the DB occlusion
		 * counters) must immediately precede every timestamp event to
		 * prevent a GPU hang on GFX9.
		 *
		 * Occlusion queries don't need to do it here, because they
		 * always do ZPASS_DONE before the timestamp.
		 */
		if (ctx->chip_class == GFX9 &&
		    query_type != PIPE_QUERY_OCCLUSION_COUNTER &&
		    query_type != PIPE_QUERY_OCCLUSION_PREDICATE &&
		    query_type != PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE) {
			struct si_resource *scratch = ctx->eop_bug_scratch;

			assert(16 * ctx->screen->info.num_render_backends <=
			       scratch->b.b.width0);
			radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 2, 0));
			radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE) | EVENT_INDEX(1));
			radeon_emit(cs, scratch->gpu_address);
			radeon_emit(cs, scratch->gpu_address >> 32);

			radeon_add_to_buffer_list(ctx, ctx->gfx_cs, scratch,
						  RADEON_USAGE_WRITE, RADEON_PRIO_QUERY);
		}

		radeon_emit(cs, PKT3(PKT3_RELEASE_MEM, 6, 0));
		radeon_emit(cs, op);
		radeon_emit(cs, sel);
		radeon_emit(cs, va);		/* address lo */
		radeon_emit(cs, va >> 32);	/* address hi */
		radeon_emit(cs, new_fence);	/* immediate data lo */
		radeon_emit(cs, 0); /* immediate data hi */
		radeon_emit(cs, 0); /* unused */
	} else {
		if (ctx->chip_class == CIK ||
		    ctx->chip_class == VI) {
			struct si_resource *scratch = ctx->eop_bug_scratch;
			uint64_t va = scratch->gpu_address;

			/* Two EOP events are required to make all engines go idle
			 * (and optional cache flushes executed) before the timestamp
			 * is written.
			 */
			radeon_emit(cs, PKT3(PKT3_EVENT_WRITE_EOP, 4, 0));
			radeon_emit(cs, op);
			radeon_emit(cs, va);
			radeon_emit(cs, ((va >> 32) & 0xffff) | sel);
			radeon_emit(cs, 0); /* immediate data */
			radeon_emit(cs, 0); /* unused */

			radeon_add_to_buffer_list(ctx, ctx->gfx_cs, scratch,
						  RADEON_USAGE_WRITE, RADEON_PRIO_QUERY);
		}

		radeon_emit(cs, PKT3(PKT3_EVENT_WRITE_EOP, 4, 0));
		radeon_emit(cs, op);
		radeon_emit(cs, va);
		radeon_emit(cs, ((va >> 32) & 0xffff) | sel);
		radeon_emit(cs, new_fence); /* immediate data */
		radeon_emit(cs, 0); /* unused */
	}

	if (buf) {
		radeon_add_to_buffer_list(ctx, ctx->gfx_cs, buf, RADEON_USAGE_WRITE,
					  RADEON_PRIO_QUERY);
	}
}

unsigned si_cp_write_fence_dwords(struct si_screen *screen)
{
	unsigned dwords = 6;

	if (screen->info.chip_class == CIK ||
	    screen->info.chip_class == VI)
		dwords *= 2;

	return dwords;
}

void si_cp_wait_mem(struct si_context *ctx, struct radeon_cmdbuf *cs,
		    uint64_t va, uint32_t ref, uint32_t mask, unsigned flags)
{
	radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));
	radeon_emit(cs, WAIT_REG_MEM_MEM_SPACE(1) | flags);
	radeon_emit(cs, va);
	radeon_emit(cs, va >> 32);
	radeon_emit(cs, ref); /* reference value */
	radeon_emit(cs, mask); /* mask */
	radeon_emit(cs, 4); /* poll interval */
}

static void si_add_fence_dependency(struct si_context *sctx,
				    struct pipe_fence_handle *fence)
{
	struct radeon_winsys *ws = sctx->ws;

	if (sctx->dma_cs)
		ws->cs_add_fence_dependency(sctx->dma_cs, fence);
	ws->cs_add_fence_dependency(sctx->gfx_cs, fence);
}

static void si_add_syncobj_signal(struct si_context *sctx,
				  struct pipe_fence_handle *fence)
{
	sctx->ws->cs_add_syncobj_signal(sctx->gfx_cs, fence);
}

static void si_fence_reference(struct pipe_screen *screen,
			       struct pipe_fence_handle **dst,
			       struct pipe_fence_handle *src)
{
	struct radeon_winsys *ws = ((struct si_screen*)screen)->ws;
	struct si_multi_fence **sdst = (struct si_multi_fence **)dst;
	struct si_multi_fence *ssrc = (struct si_multi_fence *)src;

	if (pipe_reference(&(*sdst)->reference, &ssrc->reference)) {
		ws->fence_reference(&(*sdst)->gfx, NULL);
		ws->fence_reference(&(*sdst)->sdma, NULL);
		tc_unflushed_batch_token_reference(&(*sdst)->tc_token, NULL);
		si_resource_reference(&(*sdst)->fine.buf, NULL);
		FREE(*sdst);
	}
        *sdst = ssrc;
}

static struct si_multi_fence *si_create_multi_fence()
{
	struct si_multi_fence *fence = CALLOC_STRUCT(si_multi_fence);
	if (!fence)
		return NULL;

	pipe_reference_init(&fence->reference, 1);
	util_queue_fence_init(&fence->ready);

	return fence;
}

struct pipe_fence_handle *si_create_fence(struct pipe_context *ctx,
					  struct tc_unflushed_batch_token *tc_token)
{
	struct si_multi_fence *fence = si_create_multi_fence();
	if (!fence)
		return NULL;

	util_queue_fence_reset(&fence->ready);
	tc_unflushed_batch_token_reference(&fence->tc_token, tc_token);

	return (struct pipe_fence_handle *)fence;
}

static bool si_fine_fence_signaled(struct radeon_winsys *rws,
				   const struct si_fine_fence *fine)
{
	char *map = rws->buffer_map(fine->buf->buf, NULL, PIPE_TRANSFER_READ |
							  PIPE_TRANSFER_UNSYNCHRONIZED);
	if (!map)
		return false;

	uint32_t *fence = (uint32_t*)(map + fine->offset);
	return *fence != 0;
}

static void si_fine_fence_set(struct si_context *ctx,
			      struct si_fine_fence *fine,
			      unsigned flags)
{
	uint32_t *fence_ptr;

	assert(util_bitcount(flags & (PIPE_FLUSH_TOP_OF_PIPE | PIPE_FLUSH_BOTTOM_OF_PIPE)) == 1);

	/* Use cached system memory for the fence. */
	u_upload_alloc(ctx->cached_gtt_allocator, 0, 4, 4,
		       &fine->offset, (struct pipe_resource **)&fine->buf, (void **)&fence_ptr);
	if (!fine->buf)
		return;

	*fence_ptr = 0;

	if (flags & PIPE_FLUSH_TOP_OF_PIPE) {
		uint32_t value = 0x80000000;

		si_cp_write_data(ctx, fine->buf, fine->offset, 4,
				 V_370_MEM, V_370_PFP, &value);
	} else if (flags & PIPE_FLUSH_BOTTOM_OF_PIPE) {
		uint64_t fence_va = fine->buf->gpu_address + fine->offset;

		radeon_add_to_buffer_list(ctx, ctx->gfx_cs, fine->buf,
					  RADEON_USAGE_WRITE, RADEON_PRIO_QUERY);
		si_cp_release_mem(ctx,
				  V_028A90_BOTTOM_OF_PIPE_TS, 0,
				  EOP_DST_SEL_MEM, EOP_INT_SEL_NONE,
				  EOP_DATA_SEL_VALUE_32BIT,
				  NULL, fence_va, 0x80000000,
				  PIPE_QUERY_GPU_FINISHED);
	} else {
		assert(false);
	}
}

static boolean si_fence_finish(struct pipe_screen *screen,
			       struct pipe_context *ctx,
			       struct pipe_fence_handle *fence,
			       uint64_t timeout)
{
	struct radeon_winsys *rws = ((struct si_screen*)screen)->ws;
	struct si_multi_fence *sfence = (struct si_multi_fence *)fence;
	struct si_context *sctx;
	int64_t abs_timeout = os_time_get_absolute_timeout(timeout);

	ctx = threaded_context_unwrap_sync(ctx);
	sctx = (struct si_context*)(ctx ? ctx : NULL);

	if (!util_queue_fence_is_signalled(&sfence->ready)) {
		if (sfence->tc_token) {
			/* Ensure that si_flush_from_st will be called for
			 * this fence, but only if we're in the API thread
			 * where the context is current.
			 *
			 * Note that the batch containing the flush may already
			 * be in flight in the driver thread, so the fence
			 * may not be ready yet when this call returns.
			 */
			threaded_context_flush(ctx, sfence->tc_token,
					       timeout == 0);
		}

		if (!timeout)
			return false;

		if (timeout == PIPE_TIMEOUT_INFINITE) {
			util_queue_fence_wait(&sfence->ready);
		} else {
			if (!util_queue_fence_wait_timeout(&sfence->ready, abs_timeout))
				return false;
		}

		if (timeout && timeout != PIPE_TIMEOUT_INFINITE) {
			int64_t time = os_time_get_nano();
			timeout = abs_timeout > time ? abs_timeout - time : 0;
		}
	}

	if (sfence->sdma) {
		if (!rws->fence_wait(rws, sfence->sdma, timeout))
			return false;

		/* Recompute the timeout after waiting. */
		if (timeout && timeout != PIPE_TIMEOUT_INFINITE) {
			int64_t time = os_time_get_nano();
			timeout = abs_timeout > time ? abs_timeout - time : 0;
		}
	}

	if (!sfence->gfx)
		return true;

	if (sfence->fine.buf &&
	    si_fine_fence_signaled(rws, &sfence->fine)) {
		rws->fence_reference(&sfence->gfx, NULL);
		si_resource_reference(&sfence->fine.buf, NULL);
		return true;
	}

	/* Flush the gfx IB if it hasn't been flushed yet. */
	if (sctx && sfence->gfx_unflushed.ctx == sctx &&
	    sfence->gfx_unflushed.ib_index == sctx->num_gfx_cs_flushes) {
		/* Section 4.1.2 (Signaling) of the OpenGL 4.6 (Core profile)
		 * spec says:
		 *
		 *    "If the sync object being blocked upon will not be
		 *     signaled in finite time (for example, by an associated
		 *     fence command issued previously, but not yet flushed to
		 *     the graphics pipeline), then ClientWaitSync may hang
		 *     forever. To help prevent this behavior, if
		 *     ClientWaitSync is called and all of the following are
		 *     true:
		 *
		 *     * the SYNC_FLUSH_COMMANDS_BIT bit is set in flags,
		 *     * sync is unsignaled when ClientWaitSync is called,
		 *     * and the calls to ClientWaitSync and FenceSync were
		 *       issued from the same context,
		 *
		 *     then the GL will behave as if the equivalent of Flush
		 *     were inserted immediately after the creation of sync."
		 *
		 * This means we need to flush for such fences even when we're
		 * not going to wait.
		 */
		si_flush_gfx_cs(sctx,
				(timeout ? 0 : PIPE_FLUSH_ASYNC) |
				 RADEON_FLUSH_START_NEXT_GFX_IB_NOW,
				NULL);
		sfence->gfx_unflushed.ctx = NULL;

		if (!timeout)
			return false;

		/* Recompute the timeout after all that. */
		if (timeout && timeout != PIPE_TIMEOUT_INFINITE) {
			int64_t time = os_time_get_nano();
			timeout = abs_timeout > time ? abs_timeout - time : 0;
		}
	}

	if (rws->fence_wait(rws, sfence->gfx, timeout))
		return true;

	/* Re-check in case the GPU is slow or hangs, but the commands before
	 * the fine-grained fence have completed. */
	if (sfence->fine.buf &&
	    si_fine_fence_signaled(rws, &sfence->fine))
		return true;

	return false;
}

static void si_create_fence_fd(struct pipe_context *ctx,
			       struct pipe_fence_handle **pfence, int fd,
			       enum pipe_fd_type type)
{
	struct si_screen *sscreen = (struct si_screen*)ctx->screen;
	struct radeon_winsys *ws = sscreen->ws;
	struct si_multi_fence *sfence;

	*pfence = NULL;

	sfence = si_create_multi_fence();
	if (!sfence)
		return;

	switch (type) {
	case PIPE_FD_TYPE_NATIVE_SYNC:
		if (!sscreen->info.has_fence_to_handle)
			goto finish;

		sfence->gfx = ws->fence_import_sync_file(ws, fd);
		break;

	case PIPE_FD_TYPE_SYNCOBJ:
		if (!sscreen->info.has_syncobj)
			goto finish;

		sfence->gfx = ws->fence_import_syncobj(ws, fd);
		break;

	default:
		unreachable("bad fence fd type when importing");
	}

finish:
	if (!sfence->gfx) {
		FREE(sfence);
		return;
	}

	*pfence = (struct pipe_fence_handle*)sfence;
}

static int si_fence_get_fd(struct pipe_screen *screen,
			   struct pipe_fence_handle *fence)
{
	struct si_screen *sscreen = (struct si_screen*)screen;
	struct radeon_winsys *ws = sscreen->ws;
	struct si_multi_fence *sfence = (struct si_multi_fence *)fence;
	int gfx_fd = -1, sdma_fd = -1;

	if (!sscreen->info.has_fence_to_handle)
		return -1;

	util_queue_fence_wait(&sfence->ready);

	/* Deferred fences aren't supported. */
	assert(!sfence->gfx_unflushed.ctx);
	if (sfence->gfx_unflushed.ctx)
		return -1;

	if (sfence->sdma) {
		sdma_fd = ws->fence_export_sync_file(ws, sfence->sdma);
		if (sdma_fd == -1)
			return -1;
	}
	if (sfence->gfx) {
		gfx_fd = ws->fence_export_sync_file(ws, sfence->gfx);
		if (gfx_fd == -1) {
			if (sdma_fd != -1)
				close(sdma_fd);
			return -1;
		}
	}

	/* If we don't have FDs at this point, it means we don't have fences
	 * either. */
	if (sdma_fd == -1 && gfx_fd == -1)
		return ws->export_signalled_sync_file(ws);
	if (sdma_fd == -1)
		return gfx_fd;
	if (gfx_fd == -1)
		return sdma_fd;

	/* Get a fence that will be a combination of both fences. */
	sync_accumulate("radeonsi", &gfx_fd, sdma_fd);
	close(sdma_fd);
	return gfx_fd;
}

static void si_flush_from_st(struct pipe_context *ctx,
			     struct pipe_fence_handle **fence,
			     unsigned flags)
{
	struct pipe_screen *screen = ctx->screen;
	struct si_context *sctx = (struct si_context *)ctx;
	struct radeon_winsys *ws = sctx->ws;
	struct pipe_fence_handle *gfx_fence = NULL;
	struct pipe_fence_handle *sdma_fence = NULL;
	bool deferred_fence = false;
	struct si_fine_fence fine = {};
	unsigned rflags = PIPE_FLUSH_ASYNC;

	if (flags & PIPE_FLUSH_END_OF_FRAME)
		rflags |= PIPE_FLUSH_END_OF_FRAME;

	if (flags & (PIPE_FLUSH_TOP_OF_PIPE | PIPE_FLUSH_BOTTOM_OF_PIPE)) {
		assert(flags & PIPE_FLUSH_DEFERRED);
		assert(fence);

		si_fine_fence_set(sctx, &fine, flags);
	}

	/* DMA IBs are preambles to gfx IBs, therefore must be flushed first. */
	if (sctx->dma_cs)
		si_flush_dma_cs(sctx, rflags, fence ? &sdma_fence : NULL);

	if (!radeon_emitted(sctx->gfx_cs, sctx->initial_gfx_cs_size)) {
		if (fence)
			ws->fence_reference(&gfx_fence, sctx->last_gfx_fence);
		if (!(flags & PIPE_FLUSH_DEFERRED))
			ws->cs_sync_flush(sctx->gfx_cs);
	} else {
		/* Instead of flushing, create a deferred fence. Constraints:
		 * - The state tracker must allow a deferred flush.
		 * - The state tracker must request a fence.
		 * - fence_get_fd is not allowed.
		 * Thread safety in fence_finish must be ensured by the state tracker.
		 */
		if (flags & PIPE_FLUSH_DEFERRED &&
		    !(flags & PIPE_FLUSH_FENCE_FD) &&
		    fence) {
			gfx_fence = sctx->ws->cs_get_next_fence(sctx->gfx_cs);
			deferred_fence = true;
		} else {
			si_flush_gfx_cs(sctx, rflags, fence ? &gfx_fence : NULL);
		}
	}

	/* Both engines can signal out of order, so we need to keep both fences. */
	if (fence) {
		struct si_multi_fence *multi_fence;

		if (flags & TC_FLUSH_ASYNC) {
			multi_fence = (struct si_multi_fence *)*fence;
			assert(multi_fence);
		} else {
			multi_fence = si_create_multi_fence();
			if (!multi_fence) {
				ws->fence_reference(&sdma_fence, NULL);
				ws->fence_reference(&gfx_fence, NULL);
				goto finish;
			}

			screen->fence_reference(screen, fence, NULL);
			*fence = (struct pipe_fence_handle*)multi_fence;
		}

		/* If both fences are NULL, fence_finish will always return true. */
		multi_fence->gfx = gfx_fence;
		multi_fence->sdma = sdma_fence;

		if (deferred_fence) {
			multi_fence->gfx_unflushed.ctx = sctx;
			multi_fence->gfx_unflushed.ib_index = sctx->num_gfx_cs_flushes;
		}

		multi_fence->fine = fine;
		fine.buf = NULL;

		if (flags & TC_FLUSH_ASYNC) {
			util_queue_fence_signal(&multi_fence->ready);
			tc_unflushed_batch_token_reference(&multi_fence->tc_token, NULL);
		}
	}
	assert(!fine.buf);
finish:
	if (!(flags & PIPE_FLUSH_DEFERRED)) {
		if (sctx->dma_cs)
			ws->cs_sync_flush(sctx->dma_cs);
		ws->cs_sync_flush(sctx->gfx_cs);
	}
}

static void si_fence_server_signal(struct pipe_context *ctx,
				   struct pipe_fence_handle *fence)
{
	struct si_context *sctx = (struct si_context *)ctx;
	struct si_multi_fence *sfence = (struct si_multi_fence *)fence;

	/* We should have at least one syncobj to signal */
	assert(sfence->sdma || sfence->gfx);

	if (sfence->sdma)
		si_add_syncobj_signal(sctx, sfence->sdma);
	if (sfence->gfx)
		si_add_syncobj_signal(sctx, sfence->gfx);

	/**
	 * The spec does not require a flush here. We insert a flush
	 * because syncobj based signals are not directly placed into
	 * the command stream. Instead the signal happens when the
	 * submission associated with the syncobj finishes execution.
	 *
	 * Therefore, we must make sure that we flush the pipe to avoid
	 * new work being emitted and getting executed before the signal
	 * operation.
	 */
	si_flush_from_st(ctx, NULL, PIPE_FLUSH_ASYNC);
}

static void si_fence_server_sync(struct pipe_context *ctx,
				 struct pipe_fence_handle *fence)
{
	struct si_context *sctx = (struct si_context *)ctx;
	struct si_multi_fence *sfence = (struct si_multi_fence *)fence;

	util_queue_fence_wait(&sfence->ready);

	/* Unflushed fences from the same context are no-ops. */
	if (sfence->gfx_unflushed.ctx &&
	    sfence->gfx_unflushed.ctx == sctx)
		return;

	/* All unflushed commands will not start execution before
	 * this fence dependency is signalled.
	 *
	 * Therefore we must flush before inserting the dependency
	 */
	si_flush_from_st(ctx, NULL, PIPE_FLUSH_ASYNC);

	if (sfence->sdma)
		si_add_fence_dependency(sctx, sfence->sdma);
	if (sfence->gfx)
		si_add_fence_dependency(sctx, sfence->gfx);
}

void si_init_fence_functions(struct si_context *ctx)
{
	ctx->b.flush = si_flush_from_st;
	ctx->b.create_fence_fd = si_create_fence_fd;
	ctx->b.fence_server_sync = si_fence_server_sync;
	ctx->b.fence_server_signal = si_fence_server_signal;
}

void si_init_screen_fence_functions(struct si_screen *screen)
{
	screen->b.fence_finish = si_fence_finish;
	screen->b.fence_reference = si_fence_reference;
	screen->b.fence_get_fd = si_fence_get_fd;
}