blob: 6680f1102e1a2f331f44d84a94da9bd82acdfceb [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 Hall89530822017-02-12 16:17:22 -0800176struct sync_fence_info_data *sync_fence_info(int fd)
177{
178 struct sync_fence_info_data *legacy_info;
179
180 legacy_info = legacy_sync_fence_info(fd);
181 if (legacy_info || errno != ENOTTY)
182 return legacy_info;
183
184 struct sync_file_info* file_info;
185 file_info = modern_sync_file_info(fd);
186 if (!file_info)
187 return NULL;
188 legacy_info = sync_file_info_to_legacy_fence_info(file_info);
189 sync_file_info_free(file_info);
190 return legacy_info;
Erik Gilling196b3a52012-03-07 15:30:33 -0800191}
192
193struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info,
194 struct sync_pt_info *itr)
195{
196 if (itr == NULL)
197 itr = (struct sync_pt_info *) info->pt_info;
198 else
199 itr = (struct sync_pt_info *) ((__u8 *)itr + itr->len);
200
201 if ((__u8 *)itr - (__u8 *)info >= (int)info->len)
202 return NULL;
203
204 return itr;
205}
206
207void sync_fence_info_free(struct sync_fence_info_data *info)
208{
209 free(info);
210}
211
Jesse Hall89530822017-02-12 16:17:22 -0800212void sync_file_info_free(struct sync_file_info *info)
213{
214 free(info);
215}
216
Erik Gilling196b3a52012-03-07 15:30:33 -0800217
218int sw_sync_timeline_create(void)
219{
Gustavo Padovanffc687b2016-06-10 16:51:29 -0300220 int ret;
221
222 ret = open("/sys/kernel/debug/sync/sw_sync", O_RDWR);
223 if (ret < 0)
224 ret = open("/dev/sw_sync", O_RDWR);
225
226 return ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800227}
228
229int sw_sync_timeline_inc(int fd, unsigned count)
230{
231 __u32 arg = count;
232
233 return ioctl(fd, SW_SYNC_IOC_INC, &arg);
234}
235
236int sw_sync_fence_create(int fd, const char *name, unsigned value)
237{
238 struct sw_sync_create_fence_data data;
239 int err;
240
241 data.value = value;
242 strlcpy(data.name, name, sizeof(data.name));
243
244 err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
245 if (err < 0)
246 return err;
247
248 return data.fence;
249}