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