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