iris: Convert fences to using lightweight seqno
[mesa.git] / src / gallium / drivers / iris / iris_fence.c
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 <linux/sync_file.h>
30
31 #include "util/u_inlines.h"
32 #include "intel/common/gen_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
41 gem_syncobj_create(int fd, uint32_t flags)
42 {
43 struct drm_syncobj_create args = {
44 .flags = flags,
45 };
46
47 gen_ioctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
48
49 return args.handle;
50 }
51
52 static void
53 gem_syncobj_destroy(int fd, uint32_t handle)
54 {
55 struct drm_syncobj_destroy args = {
56 .handle = handle,
57 };
58
59 gen_ioctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
60 }
61
62 /**
63 * Make a new sync-point.
64 */
65 struct iris_syncobj *
66 iris_create_syncobj(struct iris_screen *screen)
67 {
68 struct iris_syncobj *syncobj = malloc(sizeof(*syncobj));
69
70 if (!syncobj)
71 return NULL;
72
73 syncobj->handle = gem_syncobj_create(screen->fd, 0);
74 assert(syncobj->handle);
75
76 pipe_reference_init(&syncobj->ref, 1);
77
78 return syncobj;
79 }
80
81 void
82 iris_syncobj_destroy(struct iris_screen *screen, struct iris_syncobj *syncobj)
83 {
84 gem_syncobj_destroy(screen->fd, syncobj->handle);
85 free(syncobj);
86 }
87
88 /**
89 * Add a sync-point to the batch, with the given flags.
90 *
91 * \p flags One of I915_EXEC_FENCE_WAIT or I915_EXEC_FENCE_SIGNAL.
92 */
93 void
94 iris_batch_add_syncobj(struct iris_batch *batch,
95 struct iris_syncobj *syncobj,
96 unsigned flags)
97 {
98 struct drm_i915_gem_exec_fence *fence =
99 util_dynarray_grow(&batch->exec_fences, struct drm_i915_gem_exec_fence, 1);
100
101 *fence = (struct drm_i915_gem_exec_fence) {
102 .handle = syncobj->handle,
103 .flags = flags,
104 };
105
106 struct iris_syncobj **store =
107 util_dynarray_grow(&batch->syncobjs, struct iris_syncobj *, 1);
108
109 *store = NULL;
110 iris_syncobj_reference(batch->screen, store, syncobj);
111 }
112
113 /* ------------------------------------------------------------------- */
114
115 struct pipe_fence_handle {
116 struct pipe_reference ref;
117 struct iris_seqno *seqno[IRIS_BATCH_COUNT];
118 unsigned count;
119 };
120
121 static void
122 iris_fence_destroy(struct pipe_screen *p_screen,
123 struct pipe_fence_handle *fence)
124 {
125 struct iris_screen *screen = (struct iris_screen *)p_screen;
126
127 for (unsigned i = 0; i < fence->count; i++)
128 iris_seqno_reference(screen, &fence->seqno[i], NULL);
129
130 free(fence);
131 }
132
133 static void
134 iris_fence_reference(struct pipe_screen *p_screen,
135 struct pipe_fence_handle **dst,
136 struct pipe_fence_handle *src)
137 {
138 if (pipe_reference(*dst ? &(*dst)->ref : NULL,
139 src ? &src->ref : NULL))
140 iris_fence_destroy(p_screen, *dst);
141
142 *dst = src;
143 }
144
145 bool
146 iris_wait_syncobj(struct pipe_screen *p_screen,
147 struct iris_syncobj *syncobj,
148 int64_t timeout_nsec)
149 {
150 if (!syncobj)
151 return false;
152
153 struct iris_screen *screen = (struct iris_screen *)p_screen;
154 struct drm_syncobj_wait args = {
155 .handles = (uintptr_t)&syncobj->handle,
156 .count_handles = 1,
157 .timeout_nsec = timeout_nsec,
158 };
159 return gen_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
160 }
161
162 #define CSI "\e["
163 #define BLUE_HEADER CSI "0;97;44m"
164 #define NORMAL CSI "0m"
165
166 static void
167 iris_fence_flush(struct pipe_context *ctx,
168 struct pipe_fence_handle **out_fence,
169 unsigned flags)
170 {
171 struct iris_screen *screen = (void *) ctx->screen;
172 struct iris_context *ice = (struct iris_context *)ctx;
173
174 if (flags & PIPE_FLUSH_END_OF_FRAME) {
175 ice->frame++;
176
177 if (INTEL_DEBUG & DEBUG_SUBMIT) {
178 fprintf(stderr, "%s ::: FRAME %-10u (ctx %p)%-35c%s\n",
179 (INTEL_DEBUG & DEBUG_COLOR) ? BLUE_HEADER : "",
180 ice->frame, ctx, ' ',
181 (INTEL_DEBUG & DEBUG_COLOR) ? NORMAL : "");
182 }
183 }
184
185 /* XXX PIPE_FLUSH_DEFERRED */
186 for (unsigned i = 0; i < IRIS_BATCH_COUNT; i++)
187 iris_batch_flush(&ice->batches[i]);
188
189 if (!out_fence)
190 return;
191
192 struct pipe_fence_handle *fence = calloc(1, sizeof(*fence));
193 if (!fence)
194 return;
195
196 pipe_reference_init(&fence->ref, 1);
197
198 for (unsigned b = 0; b < IRIS_BATCH_COUNT; b++) {
199 struct iris_batch *batch = &ice->batches[b];
200
201 if (iris_seqno_signaled(batch->last_seqno))
202 continue;
203
204 iris_seqno_reference(screen,
205 &fence->seqno[fence->count++],
206 batch->last_seqno);
207 }
208
209 iris_fence_reference(ctx->screen, out_fence, NULL);
210 *out_fence = fence;
211 }
212
213 static void
214 iris_fence_await(struct pipe_context *ctx,
215 struct pipe_fence_handle *fence)
216 {
217 struct iris_context *ice = (struct iris_context *)ctx;
218
219 for (unsigned b = 0; b < IRIS_BATCH_COUNT; b++) {
220 struct iris_batch *batch = &ice->batches[b];
221
222 for (unsigned i = 0; i < fence->count; i++) {
223 struct iris_seqno *seqno = fence->seqno[i];
224
225 if (iris_seqno_signaled(seqno))
226 continue;
227
228 iris_batch_add_syncobj(batch, seqno->syncobj, I915_EXEC_FENCE_WAIT);
229 }
230 }
231 }
232
233 #define NSEC_PER_SEC (1000 * USEC_PER_SEC)
234 #define USEC_PER_SEC (1000 * MSEC_PER_SEC)
235 #define MSEC_PER_SEC (1000)
236
237 static uint64_t
238 gettime_ns(void)
239 {
240 struct timespec current;
241 clock_gettime(CLOCK_MONOTONIC, &current);
242 return (uint64_t)current.tv_sec * NSEC_PER_SEC + current.tv_nsec;
243 }
244
245 static uint64_t
246 rel2abs(uint64_t timeout)
247 {
248 if (timeout == 0)
249 return 0;
250
251 uint64_t current_time = gettime_ns();
252 uint64_t max_timeout = (uint64_t) INT64_MAX - current_time;
253
254 timeout = MIN2(max_timeout, timeout);
255
256 return current_time + timeout;
257 }
258
259 static bool
260 iris_fence_finish(struct pipe_screen *p_screen,
261 struct pipe_context *ctx,
262 struct pipe_fence_handle *fence,
263 uint64_t timeout)
264 {
265 struct iris_screen *screen = (struct iris_screen *)p_screen;
266
267 if (!fence->count)
268 return true;
269
270 unsigned int handle_count = 0;
271 uint32_t handles[ARRAY_SIZE(fence->seqno)];
272 for (unsigned i = 0; i < fence->count; i++) {
273 struct iris_seqno *seqno = fence->seqno[i];
274
275 if (iris_seqno_signaled(seqno))
276 continue;
277
278 handles[handle_count++] = seqno->syncobj->handle;
279 }
280
281 if (handle_count == 0)
282 return true;
283
284 struct drm_syncobj_wait args = {
285 .handles = (uintptr_t)handles,
286 .count_handles = handle_count,
287 .timeout_nsec = rel2abs(timeout),
288 .flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL
289 };
290 return gen_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_WAIT, &args) == 0;
291 }
292
293 static int
294 sync_merge_fd(int sync_fd, int new_fd)
295 {
296 if (sync_fd == -1)
297 return new_fd;
298
299 if (new_fd == -1)
300 return sync_fd;
301
302 struct sync_merge_data args = {
303 .name = "iris fence",
304 .fd2 = new_fd,
305 .fence = -1,
306 };
307
308 gen_ioctl(sync_fd, SYNC_IOC_MERGE, &args);
309 close(new_fd);
310 close(sync_fd);
311
312 return args.fence;
313 }
314
315 static int
316 iris_fence_get_fd(struct pipe_screen *p_screen,
317 struct pipe_fence_handle *fence)
318 {
319 struct iris_screen *screen = (struct iris_screen *)p_screen;
320 int fd = -1;
321
322 if (fence->count == 0) {
323 /* Our fence has no syncobj's recorded. This means that all of the
324 * batches had already completed, their syncobj's had been signalled,
325 * and so we didn't bother to record them. But we're being asked to
326 * export such a fence. So export a dummy already-signalled syncobj.
327 */
328 struct drm_syncobj_handle args = {
329 .flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE, .fd = -1,
330 };
331
332 args.handle = gem_syncobj_create(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED);
333 gen_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
334 gem_syncobj_destroy(screen->fd, args.handle);
335 return args.fd;
336 }
337
338 for (unsigned i = 0; i < fence->count; i++) {
339 struct iris_seqno *seqno = fence->seqno[i];
340
341 if (iris_seqno_signaled(seqno))
342 continue;
343
344 struct drm_syncobj_handle args = {
345 .handle = seqno->syncobj->handle,
346 .flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE,
347 .fd = -1,
348 };
349
350 gen_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
351 fd = sync_merge_fd(fd, args.fd);
352 }
353
354 return fd;
355 }
356
357 static void
358 iris_fence_create_fd(struct pipe_context *ctx,
359 struct pipe_fence_handle **out,
360 int fd,
361 enum pipe_fd_type type)
362 {
363 assert(type == PIPE_FD_TYPE_NATIVE_SYNC);
364
365 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
366 struct drm_syncobj_handle args = {
367 .handle = gem_syncobj_create(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED),
368 .flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE,
369 .fd = fd,
370 };
371 if (gen_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args) == -1) {
372 fprintf(stderr, "DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE failed: %s\n",
373 strerror(errno));
374 gem_syncobj_destroy(screen->fd, args.handle);
375 *out = NULL;
376 return;
377 }
378
379 struct iris_syncobj *syncobj = malloc(sizeof(*syncobj));
380 if (!syncobj) {
381 *out = NULL;
382 return;
383 }
384 syncobj->handle = args.handle;
385 pipe_reference_init(&syncobj->ref, 1);
386
387 struct iris_seqno *seqno = malloc(sizeof(*seqno));
388 if (!seqno) {
389 free(syncobj);
390 *out = NULL;
391 return;
392 }
393
394 static const uint32_t zero = 0;
395
396 /* Fences work in terms of iris_seqno, but we don't actually have a
397 * seqno for an imported fence. So, create a fake one which always
398 * returns as 'not signaled' so we fall back to using the sync object.
399 */
400 seqno->seqno = UINT32_MAX;
401 seqno->map = &zero;
402 seqno->syncobj = syncobj;
403 seqno->flags = IRIS_SEQNO_END;
404 pipe_reference_init(&seqno->reference, 1);
405
406 struct pipe_fence_handle *fence = malloc(sizeof(*fence));
407 if (!fence) {
408 free(seqno);
409 free(syncobj);
410 *out = NULL;
411 return;
412 }
413 pipe_reference_init(&fence->ref, 1);
414 fence->seqno[0] = seqno;
415 fence->count = 1;
416
417 *out = fence;
418 }
419
420 void
421 iris_init_screen_fence_functions(struct pipe_screen *screen)
422 {
423 screen->fence_reference = iris_fence_reference;
424 screen->fence_finish = iris_fence_finish;
425 screen->fence_get_fd = iris_fence_get_fd;
426 }
427
428 void
429 iris_init_context_fence_functions(struct pipe_context *ctx)
430 {
431 ctx->flush = iris_fence_flush;
432 ctx->create_fence_fd = iris_fence_create_fd;
433 ctx->fence_server_sync = iris_fence_await;
434 }