1 /*
2 * Copyright © 2014 Broadcom
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 /** @file v3d_fence.c
25 *
26 * Seqno-based fence management.
27 *
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.
35 */
36 #include <fcntl.h>
37 #include <libsync.h>
38
39 #include "util/u_inlines.h"
40 #include "util/os_time.h"
41
42 #include "v3d_context.h"
43 #include "v3d_bufmgr.h"
44
45 struct v3d_fence {
46 struct pipe_reference reference;
47 int fd;
48 };
49
50 static void
v3d_fence_reference(struct pipe_screen * pscreen,struct pipe_fence_handle ** pp,struct pipe_fence_handle * pf)51 v3d_fence_reference(struct pipe_screen *pscreen,
52 struct pipe_fence_handle **pp,
53 struct pipe_fence_handle *pf)
54 {
55 struct v3d_fence **p = (struct v3d_fence **)pp;
56 struct v3d_fence *f = (struct v3d_fence *)pf;
57 struct v3d_fence *old = *p;
58
59 if (pipe_reference(old ? &old->reference : NULL,
60 f ? &f->reference : NULL)) {
61 close(old->fd);
62 free(old);
63 }
64 *p = f;
65 }
66
67 void
v3d_fence_unreference(struct v3d_fence ** fence)68 v3d_fence_unreference(struct v3d_fence **fence)
69 {
70 assert(fence);
71
72 if (!*fence)
73 return;
74
75 v3d_fence_reference(NULL, (struct pipe_fence_handle **)fence, NULL);
76 }
77
78 bool
v3d_fence_wait(struct v3d_screen * screen,struct v3d_fence * fence,uint64_t timeout_ns)79 v3d_fence_wait(struct v3d_screen *screen,
80 struct v3d_fence *fence,
81 uint64_t timeout_ns)
82 {
83 int ret;
84 unsigned syncobj;
85
86 ret = drmSyncobjCreate(screen->fd, 0, &syncobj);
87 if (ret) {
88 fprintf(stderr, "Failed to create syncobj to wait on: %d\n",
89 ret);
90 return false;
91 }
92
93 ret = drmSyncobjImportSyncFile(screen->fd, syncobj, fence->fd);
94 if (ret) {
95 fprintf(stderr, "Failed to import fence to syncobj: %d\n", ret);
96 return false;
97 }
98
99 uint64_t abs_timeout = os_time_get_absolute_timeout(timeout_ns);
100 if (abs_timeout == OS_TIMEOUT_INFINITE)
101 abs_timeout = INT64_MAX;
102
103 ret = drmSyncobjWait(screen->fd, &syncobj, 1, abs_timeout, 0, NULL);
104
105 drmSyncobjDestroy(screen->fd, syncobj);
106
107 return ret >= 0;
108 }
109
110 static bool
v3d_fence_finish(struct pipe_screen * pscreen,struct pipe_context * ctx,struct pipe_fence_handle * pf,uint64_t timeout_ns)111 v3d_fence_finish(struct pipe_screen *pscreen,
112 struct pipe_context *ctx,
113 struct pipe_fence_handle *pf,
114 uint64_t timeout_ns)
115 {
116 struct v3d_screen *screen = v3d_screen(pscreen);
117 struct v3d_fence *fence = (struct v3d_fence *)pf;
118
119 return v3d_fence_wait(screen, fence, timeout_ns);
120 }
121
122 struct v3d_fence *
v3d_fence_create(struct v3d_context * v3d,int fd)123 v3d_fence_create(struct v3d_context *v3d, int fd)
124 {
125 struct v3d_fence *f = calloc(1, sizeof(*f));
126 if (!f)
127 return NULL;
128
129 f->fd = fd;
130 pipe_reference_init(&f->reference, 1);
131
132 return f;
133 }
134
135 static void
v3d_fence_create_fd(struct pipe_context * pctx,struct pipe_fence_handle ** pf,int fd,enum pipe_fd_type type)136 v3d_fence_create_fd(struct pipe_context *pctx, struct pipe_fence_handle **pf,
137 int fd, enum pipe_fd_type type)
138 {
139 struct v3d_context *v3d = (struct v3d_context *)pctx;
140 struct v3d_fence **fence = (struct v3d_fence **)pf;
141
142 assert(type == PIPE_FD_TYPE_NATIVE_SYNC);
143 *fence = v3d_fence_create(v3d, fcntl(fd, F_DUPFD_CLOEXEC, 3));
144 }
145
146 static void
v3d_fence_server_sync(struct pipe_context * pctx,struct pipe_fence_handle * pfence)147 v3d_fence_server_sync(struct pipe_context *pctx,
148 struct pipe_fence_handle *pfence)
149 {
150 struct v3d_context *v3d = (struct v3d_context*)pctx;
151 struct v3d_fence *fence = (struct v3d_fence *)pfence;
152
153 sync_accumulate("v3d", &v3d->in_fence_fd, fence->fd);
154 }
155
156 static int
v3d_fence_get_fd(struct pipe_screen * screen,struct pipe_fence_handle * pfence)157 v3d_fence_get_fd(struct pipe_screen *screen, struct pipe_fence_handle *pfence)
158 {
159 struct v3d_fence *fence = (struct v3d_fence *) pfence;
160 return fcntl(fence->fd, F_DUPFD_CLOEXEC, 3);
161 }
162
163 int
v3d_fence_context_init(struct v3d_context * v3d)164 v3d_fence_context_init(struct v3d_context *v3d)
165 {
166 v3d->base.create_fence_fd = v3d_fence_create_fd;
167 v3d->base.fence_server_sync = v3d_fence_server_sync;
168 v3d->in_fence_fd = -1;
169
170 /* Since we initialize the in_fence_fd to -1 (no wait necessary),
171 * we also need to initialize our in_syncobj as signaled.
172 */
173 return drmSyncobjCreate(v3d->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
174 &v3d->in_syncobj);
175 }
176
177 void
v3d_fence_context_finish(struct v3d_context * v3d)178 v3d_fence_context_finish(struct v3d_context *v3d)
179 {
180 drmSyncobjDestroy(v3d->fd, v3d->in_syncobj);
181 if (v3d->in_fence_fd >= 0) {
182 close(v3d->in_fence_fd);
183 v3d->in_fence_fd = -1;
184 }
185 }
186
187 void
v3d_fence_screen_init(struct v3d_screen * screen)188 v3d_fence_screen_init(struct v3d_screen *screen)
189 {
190 screen->base.fence_reference = v3d_fence_reference;
191 screen->base.fence_finish = v3d_fence_finish;
192 screen->base.fence_get_fd = v3d_fence_get_fd;
193 }
194