2 * Copyright © 2018 Intel Corporation
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
26 * Fences for driver and IPC serialisation, scheduling and synchronisation.
29 #include "util/u_inlines.h"
30 #include "intel/common/gen_gem.h"
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"
39 gem_syncobj_create(int fd
, uint32_t flags
)
41 struct drm_syncobj_create args
= {
45 gen_ioctl(fd
, DRM_IOCTL_SYNCOBJ_CREATE
, &args
);
51 gem_syncobj_destroy(int fd
, uint32_t handle
)
53 struct drm_syncobj_destroy args
= {
57 gen_ioctl(fd
, DRM_IOCTL_SYNCOBJ_DESTROY
, &args
);
61 * Make a new sync-point.
64 iris_create_syncpt(struct iris_screen
*screen
)
66 struct iris_syncpt
*syncpt
= malloc(sizeof(*syncpt
));
71 syncpt
->handle
= gem_syncobj_create(screen
->fd
, 0);
72 assert(syncpt
->handle
);
74 pipe_reference_init(&syncpt
->ref
, 1);
80 iris_syncpt_destroy(struct iris_screen
*screen
, struct iris_syncpt
*syncpt
)
82 gem_syncobj_destroy(screen
->fd
, syncpt
->handle
);
87 * Add a sync-point to the batch, with the given flags.
89 * \p flags One of I915_EXEC_FENCE_WAIT or I915_EXEC_FENCE_SIGNAL.
92 iris_batch_add_syncpt(struct iris_batch
*batch
,
93 struct iris_syncpt
*syncpt
,
96 struct drm_i915_gem_exec_fence
*fence
=
97 util_dynarray_grow(&batch
->exec_fences
, struct drm_i915_gem_exec_fence
, 1);
99 *fence
= (struct drm_i915_gem_exec_fence
) {
100 .handle
= syncpt
->handle
,
104 struct iris_syncpt
**store
=
105 util_dynarray_grow(&batch
->syncpts
, struct iris_syncpt
*, 1);
108 iris_syncpt_reference(batch
->screen
, store
, syncpt
);
111 /* ------------------------------------------------------------------- */
113 struct pipe_fence_handle
{
114 struct pipe_reference ref
;
115 struct iris_syncpt
*syncpt
[IRIS_BATCH_COUNT
];
120 iris_fence_destroy(struct pipe_screen
*p_screen
,
121 struct pipe_fence_handle
*fence
)
123 struct iris_screen
*screen
= (struct iris_screen
*)p_screen
;
125 for (unsigned i
= 0; i
< fence
->count
; i
++)
126 iris_syncpt_reference(screen
, &fence
->syncpt
[i
], NULL
);
132 iris_fence_reference(struct pipe_screen
*p_screen
,
133 struct pipe_fence_handle
**dst
,
134 struct pipe_fence_handle
*src
)
136 if (pipe_reference(*dst
? &(*dst
)->ref
: NULL
,
137 src
? &src
->ref
: NULL
))
138 iris_fence_destroy(p_screen
, *dst
);
144 iris_wait_syncpt(struct pipe_screen
*p_screen
,
145 struct iris_syncpt
*syncpt
,
146 int64_t timeout_nsec
)
151 struct iris_screen
*screen
= (struct iris_screen
*)p_screen
;
152 struct drm_syncobj_wait args
= {
153 .handles
= (uintptr_t)&syncpt
->handle
,
155 .timeout_nsec
= timeout_nsec
,
157 return gen_ioctl(screen
->fd
, DRM_IOCTL_SYNCOBJ_WAIT
, &args
);
161 #define BLUE_HEADER CSI "0;97;44m"
162 #define NORMAL CSI "0m"
165 iris_fence_flush(struct pipe_context
*ctx
,
166 struct pipe_fence_handle
**out_fence
,
169 struct iris_screen
*screen
= (void *) ctx
->screen
;
170 struct iris_context
*ice
= (struct iris_context
*)ctx
;
172 if (flags
& PIPE_FLUSH_END_OF_FRAME
) {
175 if (INTEL_DEBUG
& DEBUG_SUBMIT
) {
176 fprintf(stderr
, "%s ::: FRAME %-10u (ctx %p)%-35c%s\n",
177 (INTEL_DEBUG
& DEBUG_COLOR
) ? BLUE_HEADER
: "",
178 ice
->frame
, ctx
, ' ',
179 (INTEL_DEBUG
& DEBUG_COLOR
) ? NORMAL
: "");
183 /* XXX PIPE_FLUSH_DEFERRED */
184 for (unsigned i
= 0; i
< IRIS_BATCH_COUNT
; i
++)
185 iris_batch_flush(&ice
->batches
[i
]);
190 struct pipe_fence_handle
*fence
= calloc(1, sizeof(*fence
));
194 pipe_reference_init(&fence
->ref
, 1);
196 for (unsigned b
= 0; b
< IRIS_BATCH_COUNT
; b
++) {
197 if (!iris_wait_syncpt(ctx
->screen
, ice
->batches
[b
].last_syncpt
, 0))
200 iris_syncpt_reference(screen
, &fence
->syncpt
[fence
->count
++],
201 ice
->batches
[b
].last_syncpt
);
204 iris_fence_reference(ctx
->screen
, out_fence
, NULL
);
209 iris_fence_await(struct pipe_context
*ctx
,
210 struct pipe_fence_handle
*fence
)
212 struct iris_context
*ice
= (struct iris_context
*)ctx
;
214 for (unsigned b
= 0; b
< IRIS_BATCH_COUNT
; b
++) {
215 for (unsigned i
= 0; i
< fence
->count
; i
++) {
216 iris_batch_add_syncpt(&ice
->batches
[b
], fence
->syncpt
[i
],
217 I915_EXEC_FENCE_WAIT
);
222 #define NSEC_PER_SEC (1000 * USEC_PER_SEC)
223 #define USEC_PER_SEC (1000 * MSEC_PER_SEC)
224 #define MSEC_PER_SEC (1000)
229 struct timespec current
;
230 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
231 return (uint64_t)current
.tv_sec
* NSEC_PER_SEC
+ current
.tv_nsec
;
235 rel2abs(uint64_t timeout
)
240 uint64_t current_time
= gettime_ns();
241 uint64_t max_timeout
= (uint64_t) INT64_MAX
- current_time
;
243 timeout
= MIN2(max_timeout
, timeout
);
245 return current_time
+ timeout
;
249 iris_fence_finish(struct pipe_screen
*p_screen
,
250 struct pipe_context
*ctx
,
251 struct pipe_fence_handle
*fence
,
254 struct iris_screen
*screen
= (struct iris_screen
*)p_screen
;
259 uint32_t handles
[ARRAY_SIZE(fence
->syncpt
)];
260 for (unsigned i
= 0; i
< fence
->count
; i
++)
261 handles
[i
] = fence
->syncpt
[i
]->handle
;
263 struct drm_syncobj_wait args
= {
264 .handles
= (uintptr_t)handles
,
265 .count_handles
= fence
->count
,
266 .timeout_nsec
= rel2abs(timeout
),
267 .flags
= DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL
269 return gen_ioctl(screen
->fd
, DRM_IOCTL_SYNCOBJ_WAIT
, &args
) == 0;
272 #ifndef SYNC_IOC_MAGIC
273 /* duplicated from linux/sync_file.h to avoid build-time dependency
274 * on new (v4.7) kernel headers. Once distro's are mostly using
275 * something newer than v4.7 drop this and #include <linux/sync_file.h>
278 struct sync_merge_data
{
286 #define SYNC_IOC_MAGIC '>'
287 #define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data)
291 sync_merge_fd(int sync_fd
, int new_fd
)
299 struct sync_merge_data args
= {
300 .name
= "iris fence",
305 gen_ioctl(sync_fd
, SYNC_IOC_MERGE
, &args
);
313 iris_fence_get_fd(struct pipe_screen
*p_screen
,
314 struct pipe_fence_handle
*fence
)
316 struct iris_screen
*screen
= (struct iris_screen
*)p_screen
;
319 if (fence
->count
== 0) {
320 /* Our fence has no syncobj's recorded. This means that all of the
321 * batches had already completed, their syncobj's had been signalled,
322 * and so we didn't bother to record them. But we're being asked to
323 * export such a fence. So export a dummy already-signalled syncobj.
325 struct drm_syncobj_handle args
= {
326 .flags
= DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE
, .fd
= -1,
329 args
.handle
= gem_syncobj_create(screen
->fd
, DRM_SYNCOBJ_CREATE_SIGNALED
);
330 gen_ioctl(screen
->fd
, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD
, &args
);
331 gem_syncobj_destroy(screen
->fd
, args
.handle
);
335 for (unsigned i
= 0; i
< fence
->count
; i
++) {
336 struct drm_syncobj_handle args
= {
337 .handle
= fence
->syncpt
[i
]->handle
,
338 .flags
= DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE
,
342 gen_ioctl(screen
->fd
, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD
, &args
);
343 fd
= sync_merge_fd(fd
, args
.fd
);
350 iris_fence_create_fd(struct pipe_context
*ctx
,
351 struct pipe_fence_handle
**out
,
353 enum pipe_fd_type type
)
355 assert(type
== PIPE_FD_TYPE_NATIVE_SYNC
);
357 struct iris_screen
*screen
= (struct iris_screen
*)ctx
->screen
;
358 struct drm_syncobj_handle args
= {
359 .handle
= gem_syncobj_create(screen
->fd
, DRM_SYNCOBJ_CREATE_SIGNALED
),
360 .flags
= DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE
,
363 if (gen_ioctl(screen
->fd
, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE
, &args
) == -1) {
364 fprintf(stderr
, "DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE failed: %s\n",
366 gem_syncobj_destroy(screen
->fd
, args
.handle
);
371 struct iris_syncpt
*syncpt
= malloc(sizeof(*syncpt
));
372 syncpt
->handle
= args
.handle
;
373 pipe_reference_init(&syncpt
->ref
, 1);
375 struct pipe_fence_handle
*fence
= malloc(sizeof(*fence
));
376 pipe_reference_init(&fence
->ref
, 1);
377 fence
->syncpt
[0] = syncpt
;
384 iris_init_screen_fence_functions(struct pipe_screen
*screen
)
386 screen
->fence_reference
= iris_fence_reference
;
387 screen
->fence_finish
= iris_fence_finish
;
388 screen
->fence_get_fd
= iris_fence_get_fd
;
392 iris_init_context_fence_functions(struct pipe_context
*ctx
)
394 ctx
->flush
= iris_fence_flush
;
395 ctx
->create_fence_fd
= iris_fence_create_fd
;
396 ctx
->fence_server_sync
= iris_fence_await
;