2 * Copyright © 2014 Broadcom
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 * Seqno-based fence management.
28 * We have two mechanisms for waiting in our kernel API: You can wait on a BO
29 * to have all rendering to from any process to be completed, or wait on a
30 * seqno for that particular seqno to be passed. The fence API we're
31 * implementing is based on waiting for all rendering in the context to have
32 * completed (with no reference to what other processes might be doing with
33 * the same BOs), so we can just use the seqno of the last rendering we'd
34 * fired off as our fence marker.
40 #include "util/u_inlines.h"
42 #include "vc4_screen.h"
43 #include "vc4_context.h"
44 #include "vc4_bufmgr.h"
47 struct pipe_reference reference
;
52 static inline struct vc4_fence
*
53 vc4_fence(struct pipe_fence_handle
*pfence
)
55 return (struct vc4_fence
*)pfence
;
59 vc4_fence_reference(struct pipe_screen
*pscreen
,
60 struct pipe_fence_handle
**pp
,
61 struct pipe_fence_handle
*pf
)
63 struct vc4_fence
**p
= (struct vc4_fence
**)pp
;
64 struct vc4_fence
*f
= vc4_fence(pf
);
65 struct vc4_fence
*old
= *p
;
67 if (pipe_reference(&(*p
)->reference
, &f
->reference
)) {
76 vc4_fence_finish(struct pipe_screen
*pscreen
,
77 struct pipe_context
*ctx
,
78 struct pipe_fence_handle
*pf
,
81 struct vc4_screen
*screen
= vc4_screen(pscreen
);
82 struct vc4_fence
*f
= vc4_fence(pf
);
85 return sync_wait(f
->fd
, timeout_ns
/ 1000000) == 0;
87 return vc4_wait_seqno(screen
, f
->seqno
, timeout_ns
, "fence wait");
91 vc4_fence_create(struct vc4_screen
*screen
, uint64_t seqno
, int fd
)
93 struct vc4_fence
*f
= calloc(1, sizeof(*f
));
98 pipe_reference_init(&f
->reference
, 1);
106 vc4_fence_create_fd(struct pipe_context
*pctx
, struct pipe_fence_handle
**pf
,
107 int fd
, enum pipe_fd_type type
)
109 struct vc4_context
*vc4
= vc4_context(pctx
);
110 struct vc4_fence
**fence
= (struct vc4_fence
**)pf
;
112 assert(type
== PIPE_FD_TYPE_NATIVE_SYNC
);
113 *fence
= vc4_fence_create(vc4
->screen
, vc4
->last_emit_seqno
,
114 fcntl(fd
, F_DUPFD_CLOEXEC
, 3));
118 vc4_fence_server_sync(struct pipe_context
*pctx
,
119 struct pipe_fence_handle
*pfence
)
121 struct vc4_context
*vc4
= vc4_context(pctx
);
122 struct vc4_fence
*fence
= vc4_fence(pfence
);
125 sync_accumulate("vc4", &vc4
->in_fence_fd
, fence
->fd
);
129 vc4_fence_get_fd(struct pipe_screen
*screen
, struct pipe_fence_handle
*pfence
)
131 struct vc4_fence
*fence
= vc4_fence(pfence
);
133 return fcntl(fence
->fd
, F_DUPFD_CLOEXEC
, 3);
137 vc4_fence_context_init(struct vc4_context
*vc4
)
139 vc4
->base
.create_fence_fd
= vc4_fence_create_fd
;
140 vc4
->base
.fence_server_sync
= vc4_fence_server_sync
;
141 vc4
->in_fence_fd
= -1;
143 /* Since we initialize the in_fence_fd to -1 (no wait necessary),
144 * we also need to initialize our in_syncobj as signaled.
146 if (vc4
->screen
->has_syncobj
) {
147 return drmSyncobjCreate(vc4
->fd
, DRM_SYNCOBJ_CREATE_SIGNALED
,
155 vc4_fence_screen_init(struct vc4_screen
*screen
)
157 screen
->base
.fence_reference
= vc4_fence_reference
;
158 screen
->base
.fence_finish
= vc4_fence_finish
;
159 screen
->base
.fence_get_fd
= vc4_fence_get_fd
;