1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "pipe/p_screen.h"
30 #include "util/u_memory.h"
31 #include "util/os_file.h"
32 #include "lp_debug.h"
33 #include "lp_fence.h"
34 #include "lp_screen.h"
35 #include "lp_texture.h"
36 #include "lp_flush.h"
37 #include "lp_context.h"
38
39
40 #include "util/timespec.h"
41
42 #ifdef HAVE_LIBDRM
43 #include <xf86drm.h>
44 #include <drm-uapi/dma-buf.h>
45 #include <poll.h>
46 #include "util/libsync.h"
47 #include "util/list.h"
48 #endif
49
50 static unsigned fence_id = 0;
51
52 #ifdef HAVE_LIBDRM
sync_fd_wait(int fd,uint64_t timeout)53 static int sync_fd_wait(int fd, uint64_t timeout)
54 {
55 struct pollfd fds = {0};
56 int ret;
57 struct timespec poll_start, poll_end, timeout_ts, diff;
58 timespec_from_nsec(&timeout_ts, timeout);
59
60 fds.fd = fd;
61 fds.events = POLLIN;
62
63 do {
64 clock_gettime(CLOCK_MONOTONIC, &poll_start);
65 ret = ppoll(&fds, 1, &timeout_ts, NULL);
66 clock_gettime(CLOCK_MONOTONIC, &poll_end);
67 if (ret > 0) {
68 if (fds.revents & (POLLERR | POLLNVAL)) {
69 errno = EINVAL;
70 return -1;
71 }
72 return 0;
73 } else if (ret == 0) {
74 errno = ETIME;
75 return -1;
76 }
77
78 timespec_sub(&diff, &poll_end, &poll_start);
79 timespec_sub_saturate(&timeout_ts, &timeout_ts, &diff);
80 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
81
82 return ret;
83 }
84 #endif
85
86 /**
87 * Create a new fence object.
88 *
89 * The rank will be the number of bins in the scene. Whenever a rendering
90 * thread hits a fence command, it'll increment the fence counter. When
91 * the counter == the rank, the fence is finished.
92 *
93 * \param rank the expected finished value of the fence counter.
94 */
95 struct lp_fence *
lp_fence_create(unsigned rank)96 lp_fence_create(unsigned rank)
97 {
98 struct lp_fence *fence = CALLOC_STRUCT(lp_fence);
99
100 if (!fence)
101 return NULL;
102
103 pipe_reference_init(&fence->reference, 1);
104 fence->type = LP_FENCE_TYPE_SW;
105
106 (void) mtx_init(&fence->mutex, mtx_plain);
107 cnd_init(&fence->signalled);
108
109 fence->id = p_atomic_inc_return(&fence_id) - 1;
110 fence->rank = rank;
111
112 #ifdef HAVE_LIBDRM
113 fence->sync_fd = -1;
114 #endif
115
116 if (LP_DEBUG & DEBUG_FENCE)
117 debug_printf("%s %d\n", __func__, fence->id);
118
119 return fence;
120 }
121
122 /** Destroy a fence. Called when refcount hits zero. */
123 void
lp_fence_destroy(struct lp_fence * fence)124 lp_fence_destroy(struct lp_fence *fence)
125 {
126 if (LP_DEBUG & DEBUG_FENCE)
127 debug_printf("%s %d\n", __func__, fence->id);
128
129 if (fence->type == LP_FENCE_TYPE_SW) {
130 mtx_destroy(&fence->mutex);
131 cnd_destroy(&fence->signalled);
132 }
133 #ifdef HAVE_LIBDRM
134 else {
135 close(fence->sync_fd);
136 }
137 #endif
138
139 FREE(fence);
140 }
141
142
143 /**
144 * Called by the rendering threads to increment the fence counter.
145 * When the counter == the rank, the fence is finished.
146 */
147 void
lp_fence_signal(struct lp_fence * fence)148 lp_fence_signal(struct lp_fence *fence)
149 {
150 if (LP_DEBUG & DEBUG_FENCE)
151 debug_printf("%s %d\n", __func__, fence->id);
152
153 if (fence->type == LP_FENCE_TYPE_SW) {
154 mtx_lock(&fence->mutex);
155
156 fence->count++;
157 assert(fence->count <= fence->rank);
158
159 if (LP_DEBUG & DEBUG_FENCE)
160 debug_printf("%s count=%u rank=%u\n", __func__,
161 fence->count, fence->rank);
162
163 /* Wakeup all threads waiting on the mutex:
164 */
165 cnd_broadcast(&fence->signalled);
166
167 mtx_unlock(&fence->mutex);
168 }
169
170 /* sync fd fence we create ourselves are always signalled so
171 * we don't need an else clause
172 */
173 }
174
175
176 bool
lp_fence_signalled(struct lp_fence * f)177 lp_fence_signalled(struct lp_fence *f)
178 {
179 if (f->type == LP_FENCE_TYPE_SW)
180 return f->count == f->rank;
181 #ifdef HAVE_LIBDRM
182 else {
183 return sync_wait(f->sync_fd, 0) == 0;
184 }
185 #endif
186
187 unreachable("Fence is an unknown type");
188 return false;
189 }
190
191
192 void
lp_fence_wait(struct lp_fence * f)193 lp_fence_wait(struct lp_fence *f)
194 {
195 if (LP_DEBUG & DEBUG_FENCE)
196 debug_printf("%s %d\n", __func__, f->id);
197
198 if (f->type == LP_FENCE_TYPE_SW) {
199 mtx_lock(&f->mutex);
200 assert(f->issued);
201 while (f->count < f->rank) {
202 cnd_wait(&f->signalled, &f->mutex);
203 }
204 mtx_unlock(&f->mutex);
205 }
206 #ifdef HAVE_LIBDRM
207 else {
208 assert(f->sync_fd != -1);
209 sync_wait(f->sync_fd, -1);
210 }
211 #endif
212 }
213
214
215 bool
lp_fence_timedwait(struct lp_fence * f,uint64_t timeout)216 lp_fence_timedwait(struct lp_fence *f, uint64_t timeout)
217 {
218 struct timespec ts, abs_ts;
219
220 timespec_get(&ts, TIME_UTC);
221
222 bool ts_overflow = timespec_add_nsec(&abs_ts, &ts, timeout);
223
224 if (LP_DEBUG & DEBUG_FENCE)
225 debug_printf("%s %d\n", __func__, f->id);
226
227 if (f->type == LP_FENCE_TYPE_SW) {
228 mtx_lock(&f->mutex);
229 assert(f->issued);
230 while (f->count < f->rank) {
231 int ret;
232 if (ts_overflow)
233 ret = cnd_wait(&f->signalled, &f->mutex);
234 else
235 ret = cnd_timedwait(&f->signalled, &f->mutex, &abs_ts);
236 if (ret != thrd_success)
237 break;
238 }
239
240 const bool result = (f->count >= f->rank);
241 mtx_unlock(&f->mutex);
242 return result;
243 }
244 #ifdef HAVE_LIBDRM
245 else {
246 assert(f->sync_fd != -1);
247 return sync_fd_wait(f->sync_fd, timeout) == 0;
248 }
249 #endif
250
251 unreachable("Fence is an unknown type");
252 return false;
253 }
254
255 #ifdef HAVE_LIBDRM
256 static int
lp_fence_get_fd(struct pipe_screen * pscreen,struct pipe_fence_handle * fence)257 lp_fence_get_fd(struct pipe_screen *pscreen,
258 struct pipe_fence_handle *fence)
259 {
260 struct llvmpipe_screen *screen = llvmpipe_screen(pscreen);
261 struct lp_fence *lp_fence = (struct lp_fence *)fence;
262
263 /* It's not ideal, but since we cannot properly support sync files
264 * from userspace, what we will do instead is wait for llvmpipe to
265 * finish rendering, and then export the sync file. If its not a
266 * sync file we imported we can just export a dummy one that is always
267 * signalled since llvmpipe should have now finished all its work.
268 */
269 list_for_each_entry(struct llvmpipe_context, ctx, &screen->ctx_list, list) {
270 llvmpipe_finish((struct pipe_context *)ctx, __func__);
271 }
272
273 if (lp_fence && lp_fence->sync_fd != -1) {
274 return os_dupfd_cloexec(lp_fence->sync_fd);
275 } else if (screen->dummy_sync_fd != -1) {
276 return os_dupfd_cloexec(screen->dummy_sync_fd);
277 }
278
279 return -1;
280 }
281
282 static void
lp_create_fence_fd(struct pipe_context * pipe,struct pipe_fence_handle ** fence,int fd,enum pipe_fd_type type)283 lp_create_fence_fd(struct pipe_context *pipe,
284 struct pipe_fence_handle **fence,
285 int fd,
286 enum pipe_fd_type type)
287 {
288 /* Only sync fd are supported */
289 if (type != PIPE_FD_TYPE_NATIVE_SYNC)
290 goto fail;
291
292 struct lp_fence *f = CALLOC_STRUCT(lp_fence);
293
294 if (!fence)
295 goto fail;
296
297 pipe_reference_init(&f->reference, 1);
298 f->type = LP_FENCE_TYPE_SYNC_FD;
299 f->id = p_atomic_inc_return(&fence_id) - 1;
300 f->sync_fd = os_dupfd_cloexec(fd);
301 f->issued = true;
302
303 *fence = (struct pipe_fence_handle*)f;
304 return;
305 fail:
306 *fence = NULL;
307 return;
308 }
309
310 void
llvmpipe_init_screen_fence_funcs(struct pipe_screen * pscreen)311 llvmpipe_init_screen_fence_funcs(struct pipe_screen *pscreen)
312 {
313 struct llvmpipe_screen *screen = llvmpipe_screen(pscreen);
314 screen->dummy_sync_fd = -1;
315
316 /* Try to create dummy dmabuf, and only set functions if we were able to */
317 int fd = -1;
318 screen->dummy_dmabuf =
319 (struct llvmpipe_memory_allocation*)pscreen->allocate_memory_fd(
320 pscreen, 1, &fd, true);
321
322 /* We don't need this fd handle and API always creates it */
323 if (fd != -1)
324 close(fd);
325
326 if (screen->dummy_dmabuf) {
327 struct dma_buf_export_sync_file export = {
328 .flags = DMA_BUF_SYNC_RW,
329 .fd = -1,
330 };
331
332 if (drmIoctl(screen->dummy_dmabuf->dmabuf_fd,
333 DMA_BUF_IOCTL_EXPORT_SYNC_FILE,
334 &export))
335 goto fail;
336
337 screen->dummy_sync_fd = export.fd;
338 }
339
340 pscreen->fence_get_fd = lp_fence_get_fd;
341 return;
342 fail:
343 if (screen->dummy_dmabuf) {
344 pscreen->free_memory_fd(
345 pscreen, (struct pipe_memory_allocation*)screen->dummy_dmabuf);
346 screen->dummy_dmabuf = NULL;
347 }
348 return;
349 }
350
351 void
llvmpipe_init_fence_funcs(struct pipe_context * pipe)352 llvmpipe_init_fence_funcs(struct pipe_context *pipe)
353 {
354 pipe->create_fence_fd = lp_create_fence_fd;
355 }
356 #endif
357