1 /*
2 * Copyright © 2018 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /**
24 * @file iris_fence.c
25 *
26 * Fences for driver and IPC serialisation, scheduling and synchronisation.
27 */
28
29 #include "drm-uapi/sync_file.h"
30 #include "util/u_debug.h"
31 #include "util/u_inlines.h"
32 #include "intel/common/intel_gem.h"
33
34 #include "iris_batch.h"
35 #include "iris_bufmgr.h"
36 #include "iris_context.h"
37 #include "iris_fence.h"
38 #include "iris_screen.h"
39
40 static uint32_t
gem_syncobj_create(int fd,uint32_t flags)41 gem_syncobj_create(int fd, uint32_t flags)
42 {
43 struct drm_syncobj_create args = {
44 .flags = flags,
45 };
46
47 intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
48
49 return args.handle;
50 }
51
52 static void
gem_syncobj_destroy(int fd,uint32_t handle)53 gem_syncobj_destroy(int fd, uint32_t handle)
54 {
55 struct drm_syncobj_destroy args = {
56 .handle = handle,
57 };
58
59 intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
60 }
61
62 /**
63 * Make a new sync-point.
64 */
65 struct iris_syncobj *
iris_create_syncobj(struct iris_bufmgr * bufmgr)66 iris_create_syncobj(struct iris_bufmgr *bufmgr)
67 {
68 int fd = iris_bufmgr_get_fd(bufmgr);
69 struct iris_syncobj *syncobj = malloc(sizeof(*syncobj));
70
71 if (!syncobj)
72 return NULL;
73
74 syncobj->handle = gem_syncobj_create(fd, 0);
75 assert(syncobj->handle);
76
77 pipe_reference_init(&syncobj->ref, 1);
78
79 return syncobj;
80 }
81
82 void
iris_syncobj_destroy(struct iris_bufmgr * bufmgr,struct iris_syncobj * syncobj)83 iris_syncobj_destroy(struct iris_bufmgr *bufmgr, struct iris_syncobj *syncobj)
84 {
85 int fd = iris_bufmgr_get_fd(bufmgr);
86 gem_syncobj_destroy(fd, syncobj->handle);
87 free(syncobj);
88 }
89
90 void
iris_syncobj_signal(struct iris_bufmgr * bufmgr,struct iris_syncobj * syncobj)91 iris_syncobj_signal(struct iris_bufmgr *bufmgr, struct iris_syncobj *syncobj)
92 {
93 int fd = iris_bufmgr_get_fd(bufmgr);
94 struct drm_syncobj_array args = {
95 .handles = (uintptr_t)&syncobj->handle,
96 .count_handles = 1,
97 };
98
99 if (intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args)) {
100 fprintf(stderr, "failed to signal syncobj %"PRIu32"\n",
101 syncobj->handle);
102 }
103 }
104
105 /**
106 * Add a sync-point to the batch, with the given flags.
107 *
108 * \p flags One of IRIS_BATCH_FENCE_WAIT or IRIS_BATCH_FENCE_SIGNAL.
109 */
110 void
iris_batch_add_syncobj(struct iris_batch * batch,struct iris_syncobj * syncobj,uint32_t flags)111 iris_batch_add_syncobj(struct iris_batch *batch,
112 struct iris_syncobj *syncobj,
113 uint32_t flags)
114 {
115 struct iris_batch_fence *fence =
116 util_dynarray_grow(&batch->exec_fences, struct iris_batch_fence, 1);
117
118 *fence = (struct iris_batch_fence) {
119 .handle = syncobj->handle,
120 .flags = flags,
121 };
122
123 struct iris_syncobj **store =
124 util_dynarray_grow(&batch->syncobjs, struct iris_syncobj *, 1);
125
126 *store = NULL;
127 iris_syncobj_reference(batch->screen->bufmgr, store, syncobj);
128 }
129
130 /**
131 * Walk through a batch's dependencies (any IRIS_BATCH_FENCE_WAIT syncobjs)
132 * and unreference any which have already passed.
133 *
134 * Sometimes the compute batch is seldom used, and accumulates references
135 * to stale render batches that are no longer of interest, so we can free
136 * those up.
137 */
138 static void
clear_stale_syncobjs(struct iris_batch * batch)139 clear_stale_syncobjs(struct iris_batch *batch)
140 {
141 struct iris_screen *screen = batch->screen;
142 struct iris_bufmgr *bufmgr = screen->bufmgr;
143
144 int n = util_dynarray_num_elements(&batch->syncobjs, struct iris_syncobj *);
145
146 assert(n == util_dynarray_num_elements(&batch->exec_fences,
147 struct iris_batch_fence));
148
149 /* Skip the first syncobj, as it's the signalling one. */
150 for (int i = n - 1; i > 0; i--) {
151 struct iris_syncobj **syncobj =
152 util_dynarray_element(&batch->syncobjs, struct iris_syncobj *, i);
153 struct iris_batch_fence *fence =
154 util_dynarray_element(&batch->exec_fences,
155 struct iris_batch_fence, i);
156 assert(fence->flags & IRIS_BATCH_FENCE_WAIT);
157
158 if (iris_wait_syncobj(bufmgr, *syncobj, 0) == false)
159 continue;
160
161 /* This sync object has already passed, there's no need to continue
162 * marking it as a dependency; we can stop holding on to the reference.
163 */
164 iris_syncobj_reference(bufmgr, syncobj, NULL);
165
166 /* Remove it from the lists; move the last element here. */
167 struct iris_syncobj **nth_syncobj =
168 util_dynarray_pop_ptr(&batch->syncobjs, struct iris_syncobj *);
169 struct iris_batch_fence *nth_fence =
170 util_dynarray_pop_ptr(&batch->exec_fences, struct iris_batch_fence);
171
172 if (syncobj != nth_syncobj) {
173 *syncobj = *nth_syncobj;
174 memcpy(fence, nth_fence, sizeof(*fence));
175 }
176 }
177 }
178
179 /* ------------------------------------------------------------------- */
180
181 struct pipe_fence_handle {
182 struct pipe_reference ref;
183
184 struct pipe_context *unflushed_ctx;
185
186 struct iris_fine_fence *fine[IRIS_BATCH_COUNT];
187 };
188
189 static void
iris_fence_destroy(struct pipe_screen * p_screen,struct pipe_fence_handle * fence)190 iris_fence_destroy(struct pipe_screen *p_screen,
191 struct pipe_fence_handle *fence)
192 {
193 struct iris_screen *screen = (struct iris_screen *)p_screen;
194
195 for (unsigned i = 0; i < ARRAY_SIZE(fence->fine); i++)
196 iris_fine_fence_reference(screen, &fence->fine[i], NULL);
197
198 free(fence);
199 }
200
201 static void
iris_fence_reference(struct pipe_screen * p_screen,struct pipe_fence_handle ** dst,struct pipe_fence_handle * src)202 iris_fence_reference(struct pipe_screen *p_screen,
203 struct pipe_fence_handle **dst,
204 struct pipe_fence_handle *src)
205 {
206 if (pipe_reference(*dst ? &(*dst)->ref : NULL,
207 src ? &src->ref : NULL))
208 iris_fence_destroy(p_screen, *dst);
209
210 *dst = src;
211 }
212
213 bool
iris_wait_syncobj(struct iris_bufmgr * bufmgr,struct iris_syncobj * syncobj,int64_t timeout_nsec)214 iris_wait_syncobj(struct iris_bufmgr *bufmgr,
215 struct iris_syncobj *syncobj,
216 int64_t timeout_nsec)
217 {
218 if (!syncobj)
219 return false;
220
221 int fd = iris_bufmgr_get_fd(bufmgr);
222
223 struct drm_syncobj_wait args = {
224 .handles = (uintptr_t)&syncobj->handle,
225 .count_handles = 1,
226 .timeout_nsec = timeout_nsec,
227 };
228 return intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args) == 0;
229 }
230
231 #define CSI "\e["
232 #define BLUE_HEADER CSI "0;97;44m"
233 #define NORMAL CSI "0m"
234
235 static void
iris_fence_flush(struct pipe_context * ctx,struct pipe_fence_handle ** out_fence,unsigned flags)236 iris_fence_flush(struct pipe_context *ctx,
237 struct pipe_fence_handle **out_fence,
238 unsigned flags)
239 {
240 struct iris_screen *screen = (void *) ctx->screen;
241 struct iris_context *ice = (struct iris_context *)ctx;
242
243 /* We require DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT (kernel 5.2+) for
244 * deferred flushes. Just ignore the request to defer on older kernels.
245 */
246 if (!(screen->kernel_features & KERNEL_HAS_WAIT_FOR_SUBMIT))
247 flags &= ~PIPE_FLUSH_DEFERRED;
248
249 const bool deferred = flags & PIPE_FLUSH_DEFERRED;
250
251 if (flags & PIPE_FLUSH_END_OF_FRAME) {
252 ice->frame++;
253
254 if (INTEL_DEBUG(DEBUG_SUBMIT)) {
255 fprintf(stderr, "%s ::: FRAME %-10u (ctx %p)%-35c%s\n",
256 INTEL_DEBUG(DEBUG_COLOR) ? BLUE_HEADER : "",
257 ice->frame, ctx, ' ',
258 INTEL_DEBUG(DEBUG_COLOR) ? NORMAL : "");
259 }
260 }
261
262 iris_flush_dirty_dmabufs(ice);
263
264 if (!deferred) {
265 iris_foreach_batch(ice, batch)
266 iris_batch_flush(batch);
267 }
268
269 if (flags & PIPE_FLUSH_END_OF_FRAME) {
270 iris_measure_frame_end(ice);
271 }
272
273 intel_ds_device_process(&ice->ds, flags & PIPE_FLUSH_END_OF_FRAME);
274
275 if (!out_fence)
276 return;
277
278 struct pipe_fence_handle *fence = calloc(1, sizeof(*fence));
279 if (!fence)
280 return;
281
282 pipe_reference_init(&fence->ref, 1);
283
284 if (deferred)
285 fence->unflushed_ctx = ctx;
286
287 iris_foreach_batch(ice, batch) {
288 unsigned b = batch->name;
289
290 if (deferred && iris_batch_bytes_used(batch) > 0) {
291 struct iris_fine_fence *fine = iris_fine_fence_new(batch);
292 iris_fine_fence_reference(screen, &fence->fine[b], fine);
293 iris_fine_fence_reference(screen, &fine, NULL);
294 } else {
295 /* This batch has no commands queued up (perhaps we just flushed,
296 * or all the commands are on the other batch). Wait for the last
297 * syncobj on this engine - unless it's already finished by now.
298 */
299 if (iris_fine_fence_signaled(batch->last_fence))
300 continue;
301
302 iris_fine_fence_reference(screen, &fence->fine[b], batch->last_fence);
303 }
304 }
305
306 iris_fence_reference(ctx->screen, out_fence, NULL);
307 *out_fence = fence;
308 }
309
310 static void
iris_fence_await(struct pipe_context * ctx,struct pipe_fence_handle * fence)311 iris_fence_await(struct pipe_context *ctx,
312 struct pipe_fence_handle *fence)
313 {
314 struct iris_context *ice = (struct iris_context *)ctx;
315
316 /* Unflushed fences from the same context are no-ops. */
317 if (ctx && ctx == fence->unflushed_ctx)
318 return;
319
320 /* XXX: We can't safely flush the other context, because it might be
321 * bound to another thread, and poking at its internals wouldn't
322 * be safe. In the future we should use MI_SEMAPHORE_WAIT and
323 * block until the other job has been submitted, relying on
324 * kernel timeslicing to preempt us until the other job is
325 * actually flushed and the seqno finally passes.
326 */
327 if (fence->unflushed_ctx) {
328 util_debug_message(&ice->dbg, CONFORMANCE, "%s",
329 "glWaitSync on unflushed fence from another context "
330 "is unlikely to work without kernel 5.8+\n");
331 }
332
333 for (unsigned i = 0; i < ARRAY_SIZE(fence->fine); i++) {
334 struct iris_fine_fence *fine = fence->fine[i];
335
336 if (iris_fine_fence_signaled(fine))
337 continue;
338
339 iris_foreach_batch(ice, batch) {
340 /* We're going to make any future work in this batch wait for our
341 * fence to have gone by. But any currently queued work doesn't
342 * need to wait. Flush the batch now, so it can happen sooner.
343 */
344 iris_batch_flush(batch);
345
346 /* Before adding a new reference, clean out any stale ones. */
347 clear_stale_syncobjs(batch);
348
349 iris_batch_add_syncobj(batch, fine->syncobj, IRIS_BATCH_FENCE_WAIT);
350 }
351 }
352 }
353
354 #define NSEC_PER_SEC (1000 * USEC_PER_SEC)
355 #define USEC_PER_SEC (1000 * MSEC_PER_SEC)
356 #define MSEC_PER_SEC (1000)
357
358 static uint64_t
gettime_ns(void)359 gettime_ns(void)
360 {
361 struct timespec current;
362 clock_gettime(CLOCK_MONOTONIC, ¤t);
363 return (uint64_t)current.tv_sec * NSEC_PER_SEC + current.tv_nsec;
364 }
365
366 static uint64_t
rel2abs(uint64_t timeout)367 rel2abs(uint64_t timeout)
368 {
369 if (timeout == 0)
370 return 0;
371
372 uint64_t current_time = gettime_ns();
373 uint64_t max_timeout = (uint64_t) INT64_MAX - current_time;
374
375 timeout = MIN2(max_timeout, timeout);
376
377 return current_time + timeout;
378 }
379
380 static bool
iris_fence_finish(struct pipe_screen * p_screen,struct pipe_context * ctx,struct pipe_fence_handle * fence,uint64_t timeout)381 iris_fence_finish(struct pipe_screen *p_screen,
382 struct pipe_context *ctx,
383 struct pipe_fence_handle *fence,
384 uint64_t timeout)
385 {
386 ctx = threaded_context_unwrap_sync(ctx);
387
388 struct iris_context *ice = (struct iris_context *)ctx;
389 struct iris_screen *screen = (struct iris_screen *)p_screen;
390
391 /* If we created the fence with PIPE_FLUSH_DEFERRED, we may not have
392 * flushed yet. Check if our syncobj is the current batch's signalling
393 * syncobj - if so, we haven't flushed and need to now.
394 *
395 * The Gallium docs mention that a flush will occur if \p ctx matches
396 * the context the fence was created with. It may be NULL, so we check
397 * that it matches first.
398 */
399 if (ctx && ctx == fence->unflushed_ctx) {
400 iris_foreach_batch(ice, batch) {
401 struct iris_fine_fence *fine = fence->fine[batch->name];
402
403 if (iris_fine_fence_signaled(fine))
404 continue;
405
406 if (fine->syncobj == iris_batch_get_signal_syncobj(batch))
407 iris_batch_flush(batch);
408 }
409
410 /* The fence is no longer deferred. */
411 fence->unflushed_ctx = NULL;
412 }
413
414 unsigned int handle_count = 0;
415 uint32_t handles[ARRAY_SIZE(fence->fine)];
416 for (unsigned i = 0; i < ARRAY_SIZE(fence->fine); i++) {
417 struct iris_fine_fence *fine = fence->fine[i];
418
419 if (iris_fine_fence_signaled(fine))
420 continue;
421
422 handles[handle_count++] = fine->syncobj->handle;
423 }
424
425 if (handle_count == 0)
426 return true;
427
428 struct drm_syncobj_wait args = {
429 .handles = (uintptr_t)handles,
430 .count_handles = handle_count,
431 .timeout_nsec = rel2abs(timeout),
432 .flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL
433 };
434
435 if (fence->unflushed_ctx) {
436 /* This fence had a deferred flush from another context. We can't
437 * safely flush it here, because the context might be bound to a
438 * different thread, and poking at its internals wouldn't be safe.
439 *
440 * Instead, use the WAIT_FOR_SUBMIT flag to block and hope that
441 * another thread submits the work.
442 */
443 args.flags |= DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT;
444 }
445
446 return intel_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_WAIT, &args) == 0;
447 }
448
449 static int
sync_merge_fd(int sync_fd,int new_fd)450 sync_merge_fd(int sync_fd, int new_fd)
451 {
452 if (sync_fd == -1)
453 return new_fd;
454
455 if (new_fd == -1)
456 return sync_fd;
457
458 struct sync_merge_data args = {
459 .name = "iris fence",
460 .fd2 = new_fd,
461 .fence = -1,
462 };
463
464 intel_ioctl(sync_fd, SYNC_IOC_MERGE, &args);
465 close(new_fd);
466 close(sync_fd);
467
468 return args.fence;
469 }
470
471 static int
iris_fence_get_fd(struct pipe_screen * p_screen,struct pipe_fence_handle * fence)472 iris_fence_get_fd(struct pipe_screen *p_screen,
473 struct pipe_fence_handle *fence)
474 {
475 struct iris_screen *screen = (struct iris_screen *)p_screen;
476 int fd = -1;
477
478 /* Deferred fences aren't supported. */
479 if (fence->unflushed_ctx)
480 return -1;
481
482 for (unsigned i = 0; i < ARRAY_SIZE(fence->fine); i++) {
483 struct iris_fine_fence *fine = fence->fine[i];
484
485 if (iris_fine_fence_signaled(fine))
486 continue;
487
488 struct drm_syncobj_handle args = {
489 .handle = fine->syncobj->handle,
490 .flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE,
491 .fd = -1,
492 };
493
494 intel_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
495 fd = sync_merge_fd(fd, args.fd);
496 }
497
498 if (fd == -1) {
499 /* Our fence has no syncobj's recorded. This means that all of the
500 * batches had already completed, their syncobj's had been signalled,
501 * and so we didn't bother to record them. But we're being asked to
502 * export such a fence. So export a dummy already-signalled syncobj.
503 */
504 struct drm_syncobj_handle args = {
505 .flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE, .fd = -1,
506 };
507
508 args.handle = gem_syncobj_create(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED);
509 intel_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
510 gem_syncobj_destroy(screen->fd, args.handle);
511 return args.fd;
512 }
513
514 return fd;
515 }
516
517 static void
iris_fence_create_fd(struct pipe_context * ctx,struct pipe_fence_handle ** out,int fd,enum pipe_fd_type type)518 iris_fence_create_fd(struct pipe_context *ctx,
519 struct pipe_fence_handle **out,
520 int fd,
521 enum pipe_fd_type type)
522 {
523 assert(type == PIPE_FD_TYPE_NATIVE_SYNC || type == PIPE_FD_TYPE_SYNCOBJ);
524
525 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
526 struct drm_syncobj_handle args = {
527 .fd = fd,
528 };
529
530 if (type == PIPE_FD_TYPE_NATIVE_SYNC) {
531 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
532 args.handle = gem_syncobj_create(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED);
533 }
534
535 if (intel_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args) == -1) {
536 fprintf(stderr, "DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE failed: %s\n",
537 strerror(errno));
538 if (type == PIPE_FD_TYPE_NATIVE_SYNC)
539 gem_syncobj_destroy(screen->fd, args.handle);
540 *out = NULL;
541 return;
542 }
543
544 struct iris_syncobj *syncobj = malloc(sizeof(*syncobj));
545 if (!syncobj) {
546 *out = NULL;
547 return;
548 }
549 syncobj->handle = args.handle;
550 pipe_reference_init(&syncobj->ref, 1);
551
552 struct iris_fine_fence *fine = calloc(1, sizeof(*fine));
553 if (!fine) {
554 free(syncobj);
555 *out = NULL;
556 return;
557 }
558
559 static const uint32_t zero = 0;
560
561 /* Fences work in terms of iris_fine_fence, but we don't actually have a
562 * seqno for an imported fence. So, create a fake one which always
563 * returns as 'not signaled' so we fall back to using the sync object.
564 */
565 fine->seqno = UINT32_MAX;
566 fine->map = &zero;
567 fine->syncobj = syncobj;
568 pipe_reference_init(&fine->reference, 1);
569
570 struct pipe_fence_handle *fence = calloc(1, sizeof(*fence));
571 if (!fence) {
572 free(fine);
573 free(syncobj);
574 *out = NULL;
575 return;
576 }
577 pipe_reference_init(&fence->ref, 1);
578 fence->fine[0] = fine;
579
580 *out = fence;
581 }
582
583 static void
iris_fence_signal(struct pipe_context * ctx,struct pipe_fence_handle * fence)584 iris_fence_signal(struct pipe_context *ctx,
585 struct pipe_fence_handle *fence)
586 {
587 struct iris_context *ice = (struct iris_context *)ctx;
588
589 if (ctx == fence->unflushed_ctx)
590 return;
591
592 iris_foreach_batch(ice, batch) {
593 for (unsigned i = 0; i < ARRAY_SIZE(fence->fine); i++) {
594 struct iris_fine_fence *fine = fence->fine[i];
595
596 /* already signaled fence skipped */
597 if (iris_fine_fence_signaled(fine))
598 continue;
599
600 batch->contains_fence_signal = true;
601 iris_batch_add_syncobj(batch, fine->syncobj, IRIS_BATCH_FENCE_SIGNAL);
602 }
603 if (batch->contains_fence_signal)
604 iris_batch_flush(batch);
605 }
606 }
607
608 void
iris_init_screen_fence_functions(struct pipe_screen * screen)609 iris_init_screen_fence_functions(struct pipe_screen *screen)
610 {
611 screen->fence_reference = iris_fence_reference;
612 screen->fence_finish = iris_fence_finish;
613 screen->fence_get_fd = iris_fence_get_fd;
614 }
615
616 void
iris_init_context_fence_functions(struct pipe_context * ctx)617 iris_init_context_fence_functions(struct pipe_context *ctx)
618 {
619 ctx->flush = iris_fence_flush;
620 ctx->create_fence_fd = iris_fence_create_fd;
621 ctx->fence_server_sync = iris_fence_await;
622 ctx->fence_server_signal = iris_fence_signal;
623 }
624