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