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