blob: f3e26e1115e46729326821964c7f65b6444a0709 [file] [log] [blame]
Erik Gilling196b3a52012-03-07 15:30:33 -08001/*
2 * sync.c
3 *
4 * Copyright 2012 Google, Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include <fcntl.h>
Elliott Hughesa744b052015-01-28 11:37:57 -080020#include <malloc.h>
Erik Gilling196b3a52012-03-07 15:30:33 -080021#include <stdint.h>
22#include <string.h>
Gustavo Padovan61ab0d72016-06-11 11:11:19 -030023#include <errno.h>
24#include <poll.h>
Erik Gilling196b3a52012-03-07 15:30:33 -080025
26#include <sys/ioctl.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29
Christopher Ferrisf83c7922016-08-24 14:49:18 -070030#include <sync/sync.h>
31
Christopher Ferrisf83c7922016-08-24 14:49:18 -070032
Christopher Ferris1514bb42016-12-12 17:32:55 -080033struct sw_sync_create_fence_data {
34 __u32 value;
35 char name[32];
36 __s32 fence;
37};
38
39#define SW_SYNC_IOC_MAGIC 'W'
40#define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0, struct sw_sync_create_fence_data)
41#define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
42
Erik Gilling984d3572012-08-21 18:21:18 -070043int sync_wait(int fd, int timeout)
Erik Gilling196b3a52012-03-07 15:30:33 -080044{
Gustavo Padovan61ab0d72016-06-11 11:11:19 -030045 struct pollfd fds;
46 int ret;
Erik Gilling196b3a52012-03-07 15:30:33 -080047
Gustavo Padovan61ab0d72016-06-11 11:11:19 -030048 if (fd < 0) {
49 errno = EINVAL;
50 return -1;
51 }
52
53 fds.fd = fd;
54 fds.events = POLLIN;
55
56 do {
57 ret = poll(&fds, 1, timeout);
58 if (ret > 0) {
59 if (fds.revents & (POLLERR | POLLNVAL)) {
60 errno = EINVAL;
61 return -1;
62 }
63 return 0;
64 } else if (ret == 0) {
65 errno = ETIME;
66 return -1;
67 }
68 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
69
70 return ret;
Erik Gilling196b3a52012-03-07 15:30:33 -080071}
72
73int sync_merge(const char *name, int fd1, int fd2)
74{
Gustavo Padovan61ab0d72016-06-11 11:11:19 -030075 struct sync_legacy_merge_data legacy_data;
76 struct sync_merge_data data;
77 int ret;
Erik Gilling196b3a52012-03-07 15:30:33 -080078
79 data.fd2 = fd2;
80 strlcpy(data.name, name, sizeof(data.name));
Gustavo Padovan61ab0d72016-06-11 11:11:19 -030081 data.flags = 0;
82 data.pad = 0;
Erik Gilling196b3a52012-03-07 15:30:33 -080083
Gustavo Padovan61ab0d72016-06-11 11:11:19 -030084 ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
85 if (ret < 0 && errno == ENOTTY) {
86 legacy_data.fd2 = fd2;
87 strlcpy(legacy_data.name, name, sizeof(legacy_data.name));
88
89 ret = ioctl(fd1, SYNC_IOC_LEGACY_MERGE, &legacy_data);
90 if (ret < 0)
91 return ret;
92
93 return legacy_data.fence;
94 } else if (ret < 0) {
95 return ret;
96 }
Erik Gilling196b3a52012-03-07 15:30:33 -080097
98 return data.fence;
99}
100
Jesse Hall89530822017-02-12 16:17:22 -0800101static struct sync_fence_info_data *legacy_sync_fence_info(int fd)
Erik Gilling196b3a52012-03-07 15:30:33 -0800102{
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300103 struct sync_fence_info_data *legacy_info;
104 struct sync_pt_info *legacy_pt_info;
Jesse Hall89530822017-02-12 16:17:22 -0800105 int err;
Erik Gilling196b3a52012-03-07 15:30:33 -0800106
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300107 legacy_info = malloc(4096);
108 if (legacy_info == NULL)
Erik Gilling196b3a52012-03-07 15:30:33 -0800109 return NULL;
110
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300111 legacy_info->len = 4096;
112 err = ioctl(fd, SYNC_IOC_LEGACY_FENCE_INFO, legacy_info);
Jesse Hall89530822017-02-12 16:17:22 -0800113 if (err < 0) {
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300114 free(legacy_info);
Erik Gilling196b3a52012-03-07 15:30:33 -0800115 return NULL;
116 }
Jesse Hall89530822017-02-12 16:17:22 -0800117 return legacy_info;
118}
Erik Gilling196b3a52012-03-07 15:30:33 -0800119
Jesse Hall89530822017-02-12 16:17:22 -0800120static struct sync_file_info *modern_sync_file_info(int fd)
121{
122 struct sync_file_info local_info;
123 struct sync_file_info *info;
124 int err;
125
126 memset(&local_info, 0, sizeof(local_info));
127 err = ioctl(fd, SYNC_IOC_FILE_INFO, &local_info);
128 if (err < 0)
129 return NULL;
130
131 info = calloc(1, sizeof(struct sync_file_info) +
132 local_info.num_fences * sizeof(struct sync_fence_info));
133 if (!info)
134 return NULL;
135 info->sync_fence_info = (__u64)(uintptr_t)(info + 1);
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300136
137 err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
Jesse Hall89530822017-02-12 16:17:22 -0800138 if (err < 0) {
139 free(info);
140 return NULL;
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300141 }
142
Jesse Hall89530822017-02-12 16:17:22 -0800143 return info;
144}
145
146static struct sync_fence_info_data *sync_file_info_to_legacy_fence_info(
147 const struct sync_file_info *info)
148{
149 struct sync_fence_info_data *legacy_info;
150 struct sync_pt_info *legacy_pt_info;
151 const struct sync_fence_info *fence_info = sync_get_fence_info(info);
152 const uint32_t num_fences = info->num_fences;
153
154 legacy_info = malloc(4096);
155 if (legacy_info == NULL)
156 return NULL;
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300157 legacy_info->len = sizeof(*legacy_info) +
Jesse Hall077ffd52017-02-12 16:01:36 -0800158 num_fences * sizeof(struct sync_pt_info);
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300159 strlcpy(legacy_info->name, info->name, sizeof(legacy_info->name));
160 legacy_info->status = info->status;
161
162 legacy_pt_info = (struct sync_pt_info *)legacy_info->pt_info;
Jesse Hall89530822017-02-12 16:17:22 -0800163 for (uint32_t i = 0; i < num_fences; i++) {
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300164 legacy_pt_info[i].len = sizeof(*legacy_pt_info);
165 strlcpy(legacy_pt_info[i].obj_name, fence_info[i].obj_name,
166 sizeof(legacy_pt_info->obj_name));
167 strlcpy(legacy_pt_info[i].driver_name, fence_info[i].driver_name,
168 sizeof(legacy_pt_info->driver_name));
169 legacy_pt_info[i].status = fence_info[i].status;
170 legacy_pt_info[i].timestamp_ns = fence_info[i].timestamp_ns;
171 }
172
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300173 return legacy_info;
Jesse Hall89530822017-02-12 16:17:22 -0800174}
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300175
Jesse Hall83666162017-02-12 16:32:39 -0800176static struct sync_file_info* legacy_fence_info_to_sync_file_info(
177 struct sync_fence_info_data *legacy_info)
178{
179 struct sync_file_info *info;
180 struct sync_pt_info *pt;
181 struct sync_fence_info *fence;
182 size_t num_fences;
183 int err;
184
185 pt = NULL;
186 num_fences = 0;
187 while ((pt = sync_pt_info(legacy_info, pt)) != NULL)
188 num_fences++;
189
190 info = calloc(1, sizeof(struct sync_file_info) +
191 num_fences * sizeof(struct sync_fence_info));
192 if (!info) {
193 free(legacy_info);
194 return NULL;
195 }
196 info->sync_fence_info = (__u64)(uintptr_t)(info + 1);
197
198 strlcpy(info->name, legacy_info->name, sizeof(info->name));
199 info->status = legacy_info->status;
200 info->num_fences = num_fences;
201
202 pt = NULL;
203 fence = sync_get_fence_info(info);
204 while ((pt = sync_pt_info(legacy_info, pt)) != NULL) {
205 strlcpy(fence->obj_name, pt->obj_name, sizeof(fence->obj_name));
206 strlcpy(fence->driver_name, pt->driver_name,
207 sizeof(fence->driver_name));
208 fence->status = pt->status;
209 fence->timestamp_ns = pt->timestamp_ns;
210 fence++;
211 }
212
213 return info;
214}
215
Jesse Hall89530822017-02-12 16:17:22 -0800216struct sync_fence_info_data *sync_fence_info(int fd)
217{
218 struct sync_fence_info_data *legacy_info;
219
220 legacy_info = legacy_sync_fence_info(fd);
221 if (legacy_info || errno != ENOTTY)
222 return legacy_info;
223
224 struct sync_file_info* file_info;
225 file_info = modern_sync_file_info(fd);
226 if (!file_info)
227 return NULL;
228 legacy_info = sync_file_info_to_legacy_fence_info(file_info);
229 sync_file_info_free(file_info);
230 return legacy_info;
Erik Gilling196b3a52012-03-07 15:30:33 -0800231}
232
Jesse Hall83666162017-02-12 16:32:39 -0800233struct sync_file_info* sync_file_info(int32_t fd)
234{
235 struct sync_file_info *info;
236 struct sync_fence_info_data *legacy_info;
237
238 info = modern_sync_file_info(fd);
239 if (info || errno != ENOTTY)
240 return info;
241
242 legacy_info = legacy_sync_fence_info(fd);
243 if (!legacy_info)
244 return NULL;
245 info = legacy_fence_info_to_sync_file_info(legacy_info);
246 sync_fence_info_free(legacy_info);
247 return info;
248}
249
Erik Gilling196b3a52012-03-07 15:30:33 -0800250struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info,
251 struct sync_pt_info *itr)
252{
253 if (itr == NULL)
254 itr = (struct sync_pt_info *) info->pt_info;
255 else
256 itr = (struct sync_pt_info *) ((__u8 *)itr + itr->len);
257
258 if ((__u8 *)itr - (__u8 *)info >= (int)info->len)
259 return NULL;
260
261 return itr;
262}
263
264void sync_fence_info_free(struct sync_fence_info_data *info)
265{
266 free(info);
267}
268
Jesse Hall89530822017-02-12 16:17:22 -0800269void sync_file_info_free(struct sync_file_info *info)
270{
271 free(info);
272}
273
Erik Gilling196b3a52012-03-07 15:30:33 -0800274
275int sw_sync_timeline_create(void)
276{
Gustavo Padovanffc687b2016-06-10 16:51:29 -0300277 int ret;
278
279 ret = open("/sys/kernel/debug/sync/sw_sync", O_RDWR);
280 if (ret < 0)
281 ret = open("/dev/sw_sync", O_RDWR);
282
283 return ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800284}
285
286int sw_sync_timeline_inc(int fd, unsigned count)
287{
288 __u32 arg = count;
289
290 return ioctl(fd, SW_SYNC_IOC_INC, &arg);
291}
292
293int sw_sync_fence_create(int fd, const char *name, unsigned value)
294{
295 struct sw_sync_create_fence_data data;
296 int err;
297
298 data.value = value;
299 strlcpy(data.name, name, sizeof(data.name));
300
301 err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
302 if (err < 0)
303 return err;
304
305 return data.fence;
306}