blob: b8c48c7f7602f39cf4c72ef15f5b3604786d438a [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
Jesse Hall6cd0fc52017-02-18 21:51:04 -080019#include <errno.h>
Erik Gilling196b3a52012-03-07 15:30:33 -080020#include <fcntl.h>
Elliott Hughesa744b052015-01-28 11:37:57 -080021#include <malloc.h>
Jesse Hall6cd0fc52017-02-18 21:51:04 -080022#include <poll.h>
23#include <stdatomic.h>
Erik Gilling196b3a52012-03-07 15:30:33 -080024#include <stdint.h>
25#include <string.h>
26
27#include <sys/ioctl.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30
Jesse Hall41129a22017-02-13 15:19:24 -080031#include <android/sync.h>
Christopher Ferrisf83c7922016-08-24 14:49:18 -070032
Jesse Hall82d377b2018-06-13 11:52:10 -070033/* Prototypes for deprecated functions that used to be declared in the legacy
34 * android/sync.h. They've been moved here to make sure new code does not use
35 * them, but the functions are still defined to avoid breaking existing
36 * binaries. Eventually they can be removed altogether.
37 */
38struct sync_fence_info_data {
39 uint32_t len;
40 char name[32];
41 int32_t status;
42 uint8_t pt_info[0];
43};
44struct sync_pt_info {
45 uint32_t len;
46 char obj_name[32];
47 char driver_name[32];
48 int32_t status;
49 uint64_t timestamp_ns;
50 uint8_t driver_data[0];
51};
52struct sync_fence_info_data* sync_fence_info(int fd);
53struct sync_pt_info* sync_pt_info(struct sync_fence_info_data* info, struct sync_pt_info* itr);
54void sync_fence_info_free(struct sync_fence_info_data* info);
55
Jesse Hallb7fdb2a2017-02-12 16:42:11 -080056/* Legacy Sync API */
57
58struct sync_legacy_merge_data {
59 int32_t fd2;
60 char name[32];
61 int32_t fence;
62};
63
64/**
65 * DOC: SYNC_IOC_MERGE - merge two fences
66 *
67 * Takes a struct sync_merge_data. Creates a new fence containing copies of
68 * the sync_pts in both the calling fd and sync_merge_data.fd2. Returns the
69 * new fence's fd in sync_merge_data.fence
70 *
71 * This is the legacy version of the Sync API before the de-stage that happened
72 * on Linux kernel 4.7.
73 */
74#define SYNC_IOC_LEGACY_MERGE _IOWR(SYNC_IOC_MAGIC, 1, \
75 struct sync_legacy_merge_data)
76
77/**
78 * DOC: SYNC_IOC_LEGACY_FENCE_INFO - get detailed information on a fence
79 *
80 * Takes a struct sync_fence_info_data with extra space allocated for pt_info.
81 * Caller should write the size of the buffer into len. On return, len is
82 * updated to reflect the total size of the sync_fence_info_data including
83 * pt_info.
84 *
85 * pt_info is a buffer containing sync_pt_infos for every sync_pt in the fence.
86 * To iterate over the sync_pt_infos, use the sync_pt_info.len field.
87 *
88 * This is the legacy version of the Sync API before the de-stage that happened
89 * on Linux kernel 4.7.
90 */
91#define SYNC_IOC_LEGACY_FENCE_INFO _IOWR(SYNC_IOC_MAGIC, 2,\
92 struct sync_fence_info_data)
93
94/* SW Sync API */
Christopher Ferrisf83c7922016-08-24 14:49:18 -070095
Christopher Ferris1514bb42016-12-12 17:32:55 -080096struct sw_sync_create_fence_data {
97 __u32 value;
98 char name[32];
99 __s32 fence;
100};
101
102#define SW_SYNC_IOC_MAGIC 'W'
103#define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0, struct sw_sync_create_fence_data)
104#define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
105
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800106// ---------------------------------------------------------------------------
107// Support for caching the sync uapi version.
108//
109// This library supports both legacy (android/staging) uapi and modern
110// (mainline) sync uapi. Library calls first try one uapi, and if that fails,
111// try the other. Since any given kernel only supports one uapi version, after
112// the first successful syscall we know what the kernel supports and can skip
113// trying the other.
114
115enum uapi_version {
116 UAPI_UNKNOWN,
117 UAPI_MODERN,
118 UAPI_LEGACY
119};
120static atomic_int g_uapi_version = ATOMIC_VAR_INIT(UAPI_UNKNOWN);
121
122// ---------------------------------------------------------------------------
123
Erik Gilling984d3572012-08-21 18:21:18 -0700124int sync_wait(int fd, int timeout)
Erik Gilling196b3a52012-03-07 15:30:33 -0800125{
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300126 struct pollfd fds;
127 int ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800128
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300129 if (fd < 0) {
130 errno = EINVAL;
131 return -1;
132 }
133
134 fds.fd = fd;
135 fds.events = POLLIN;
136
137 do {
138 ret = poll(&fds, 1, timeout);
139 if (ret > 0) {
140 if (fds.revents & (POLLERR | POLLNVAL)) {
141 errno = EINVAL;
142 return -1;
143 }
144 return 0;
145 } else if (ret == 0) {
146 errno = ETIME;
147 return -1;
148 }
149 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
150
151 return ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800152}
153
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800154static int legacy_sync_merge(const char *name, int fd1, int fd2)
Erik Gilling196b3a52012-03-07 15:30:33 -0800155{
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800156 struct sync_legacy_merge_data data;
157 int ret;
158
159 data.fd2 = fd2;
160 strlcpy(data.name, name, sizeof(data.name));
161 ret = ioctl(fd1, SYNC_IOC_LEGACY_MERGE, &data);
162 if (ret < 0)
163 return ret;
164 return data.fence;
165}
166
167static int modern_sync_merge(const char *name, int fd1, int fd2)
168{
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300169 struct sync_merge_data data;
170 int ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800171
172 data.fd2 = fd2;
173 strlcpy(data.name, name, sizeof(data.name));
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300174 data.flags = 0;
175 data.pad = 0;
Erik Gilling196b3a52012-03-07 15:30:33 -0800176
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300177 ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800178 if (ret < 0)
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300179 return ret;
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800180 return data.fence;
181}
182
183int sync_merge(const char *name, int fd1, int fd2)
184{
185 int uapi;
186 int ret;
187
188 uapi = atomic_load_explicit(&g_uapi_version, memory_order_acquire);
189
190 if (uapi == UAPI_MODERN || uapi == UAPI_UNKNOWN) {
191 ret = modern_sync_merge(name, fd1, fd2);
192 if (ret >= 0 || errno != ENOTTY) {
193 if (ret >= 0 && uapi == UAPI_UNKNOWN) {
194 atomic_store_explicit(&g_uapi_version, UAPI_MODERN,
195 memory_order_release);
196 }
197 return ret;
198 }
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300199 }
Erik Gilling196b3a52012-03-07 15:30:33 -0800200
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800201 ret = legacy_sync_merge(name, fd1, fd2);
202 if (ret >= 0 && uapi == UAPI_UNKNOWN) {
203 atomic_store_explicit(&g_uapi_version, UAPI_LEGACY,
204 memory_order_release);
205 }
206 return ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800207}
208
Jesse Hall89530822017-02-12 16:17:22 -0800209static struct sync_fence_info_data *legacy_sync_fence_info(int fd)
Erik Gilling196b3a52012-03-07 15:30:33 -0800210{
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300211 struct sync_fence_info_data *legacy_info;
212 struct sync_pt_info *legacy_pt_info;
Jesse Hall89530822017-02-12 16:17:22 -0800213 int err;
Erik Gilling196b3a52012-03-07 15:30:33 -0800214
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300215 legacy_info = malloc(4096);
216 if (legacy_info == NULL)
Erik Gilling196b3a52012-03-07 15:30:33 -0800217 return NULL;
218
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300219 legacy_info->len = 4096;
220 err = ioctl(fd, SYNC_IOC_LEGACY_FENCE_INFO, legacy_info);
Jesse Hall89530822017-02-12 16:17:22 -0800221 if (err < 0) {
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300222 free(legacy_info);
Erik Gilling196b3a52012-03-07 15:30:33 -0800223 return NULL;
224 }
Jesse Hall89530822017-02-12 16:17:22 -0800225 return legacy_info;
226}
Erik Gilling196b3a52012-03-07 15:30:33 -0800227
Jesse Hall89530822017-02-12 16:17:22 -0800228static struct sync_file_info *modern_sync_file_info(int fd)
229{
230 struct sync_file_info local_info;
231 struct sync_file_info *info;
232 int err;
233
234 memset(&local_info, 0, sizeof(local_info));
235 err = ioctl(fd, SYNC_IOC_FILE_INFO, &local_info);
236 if (err < 0)
237 return NULL;
238
239 info = calloc(1, sizeof(struct sync_file_info) +
240 local_info.num_fences * sizeof(struct sync_fence_info));
241 if (!info)
242 return NULL;
Saurabh Shah90a74602017-08-01 13:54:21 -0700243
244 info->num_fences = local_info.num_fences;
Jesse Hall89530822017-02-12 16:17:22 -0800245 info->sync_fence_info = (__u64)(uintptr_t)(info + 1);
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300246
247 err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
Jesse Hall89530822017-02-12 16:17:22 -0800248 if (err < 0) {
249 free(info);
250 return NULL;
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300251 }
252
Jesse Hall89530822017-02-12 16:17:22 -0800253 return info;
254}
255
256static struct sync_fence_info_data *sync_file_info_to_legacy_fence_info(
257 const struct sync_file_info *info)
258{
259 struct sync_fence_info_data *legacy_info;
260 struct sync_pt_info *legacy_pt_info;
261 const struct sync_fence_info *fence_info = sync_get_fence_info(info);
262 const uint32_t num_fences = info->num_fences;
263
264 legacy_info = malloc(4096);
265 if (legacy_info == NULL)
266 return NULL;
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300267 legacy_info->len = sizeof(*legacy_info) +
Jesse Hall077ffd52017-02-12 16:01:36 -0800268 num_fences * sizeof(struct sync_pt_info);
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300269 strlcpy(legacy_info->name, info->name, sizeof(legacy_info->name));
270 legacy_info->status = info->status;
271
272 legacy_pt_info = (struct sync_pt_info *)legacy_info->pt_info;
Jesse Hall89530822017-02-12 16:17:22 -0800273 for (uint32_t i = 0; i < num_fences; i++) {
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300274 legacy_pt_info[i].len = sizeof(*legacy_pt_info);
275 strlcpy(legacy_pt_info[i].obj_name, fence_info[i].obj_name,
276 sizeof(legacy_pt_info->obj_name));
277 strlcpy(legacy_pt_info[i].driver_name, fence_info[i].driver_name,
278 sizeof(legacy_pt_info->driver_name));
279 legacy_pt_info[i].status = fence_info[i].status;
280 legacy_pt_info[i].timestamp_ns = fence_info[i].timestamp_ns;
281 }
282
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300283 return legacy_info;
Jesse Hall89530822017-02-12 16:17:22 -0800284}
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300285
Jesse Hall83666162017-02-12 16:32:39 -0800286static struct sync_file_info* legacy_fence_info_to_sync_file_info(
287 struct sync_fence_info_data *legacy_info)
288{
289 struct sync_file_info *info;
290 struct sync_pt_info *pt;
291 struct sync_fence_info *fence;
292 size_t num_fences;
293 int err;
294
295 pt = NULL;
296 num_fences = 0;
297 while ((pt = sync_pt_info(legacy_info, pt)) != NULL)
298 num_fences++;
299
300 info = calloc(1, sizeof(struct sync_file_info) +
301 num_fences * sizeof(struct sync_fence_info));
302 if (!info) {
Jesse Hall83666162017-02-12 16:32:39 -0800303 return NULL;
304 }
305 info->sync_fence_info = (__u64)(uintptr_t)(info + 1);
306
307 strlcpy(info->name, legacy_info->name, sizeof(info->name));
308 info->status = legacy_info->status;
309 info->num_fences = num_fences;
310
311 pt = NULL;
312 fence = sync_get_fence_info(info);
313 while ((pt = sync_pt_info(legacy_info, pt)) != NULL) {
314 strlcpy(fence->obj_name, pt->obj_name, sizeof(fence->obj_name));
315 strlcpy(fence->driver_name, pt->driver_name,
316 sizeof(fence->driver_name));
317 fence->status = pt->status;
318 fence->timestamp_ns = pt->timestamp_ns;
319 fence++;
320 }
321
322 return info;
323}
324
Jesse Hall89530822017-02-12 16:17:22 -0800325struct sync_fence_info_data *sync_fence_info(int fd)
326{
327 struct sync_fence_info_data *legacy_info;
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800328 int uapi;
Jesse Hall89530822017-02-12 16:17:22 -0800329
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800330 uapi = atomic_load_explicit(&g_uapi_version, memory_order_acquire);
331
332 if (uapi == UAPI_LEGACY || uapi == UAPI_UNKNOWN) {
333 legacy_info = legacy_sync_fence_info(fd);
334 if (legacy_info || errno != ENOTTY) {
335 if (legacy_info && uapi == UAPI_UNKNOWN) {
336 atomic_store_explicit(&g_uapi_version, UAPI_LEGACY,
337 memory_order_release);
338 }
339 return legacy_info;
340 }
341 }
Jesse Hall89530822017-02-12 16:17:22 -0800342
343 struct sync_file_info* file_info;
344 file_info = modern_sync_file_info(fd);
345 if (!file_info)
346 return NULL;
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800347 if (uapi == UAPI_UNKNOWN) {
348 atomic_store_explicit(&g_uapi_version, UAPI_MODERN,
349 memory_order_release);
350 }
Jesse Hall89530822017-02-12 16:17:22 -0800351 legacy_info = sync_file_info_to_legacy_fence_info(file_info);
352 sync_file_info_free(file_info);
353 return legacy_info;
Erik Gilling196b3a52012-03-07 15:30:33 -0800354}
355
Jesse Hall83666162017-02-12 16:32:39 -0800356struct sync_file_info* sync_file_info(int32_t fd)
357{
358 struct sync_file_info *info;
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800359 int uapi;
360
361 uapi = atomic_load_explicit(&g_uapi_version, memory_order_acquire);
362
363 if (uapi == UAPI_MODERN || uapi == UAPI_UNKNOWN) {
364 info = modern_sync_file_info(fd);
365 if (info || errno != ENOTTY) {
366 if (info && uapi == UAPI_UNKNOWN) {
367 atomic_store_explicit(&g_uapi_version, UAPI_MODERN,
368 memory_order_release);
369 }
370 return info;
371 }
372 }
373
Jesse Hall83666162017-02-12 16:32:39 -0800374 struct sync_fence_info_data *legacy_info;
Jesse Hall83666162017-02-12 16:32:39 -0800375 legacy_info = legacy_sync_fence_info(fd);
376 if (!legacy_info)
377 return NULL;
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800378 if (uapi == UAPI_UNKNOWN) {
379 atomic_store_explicit(&g_uapi_version, UAPI_LEGACY,
380 memory_order_release);
381 }
Jesse Hall83666162017-02-12 16:32:39 -0800382 info = legacy_fence_info_to_sync_file_info(legacy_info);
383 sync_fence_info_free(legacy_info);
384 return info;
385}
386
Erik Gilling196b3a52012-03-07 15:30:33 -0800387struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info,
388 struct sync_pt_info *itr)
389{
390 if (itr == NULL)
391 itr = (struct sync_pt_info *) info->pt_info;
392 else
393 itr = (struct sync_pt_info *) ((__u8 *)itr + itr->len);
394
395 if ((__u8 *)itr - (__u8 *)info >= (int)info->len)
396 return NULL;
397
398 return itr;
399}
400
401void sync_fence_info_free(struct sync_fence_info_data *info)
402{
403 free(info);
404}
405
Jesse Hall89530822017-02-12 16:17:22 -0800406void sync_file_info_free(struct sync_file_info *info)
407{
408 free(info);
409}
410
Erik Gilling196b3a52012-03-07 15:30:33 -0800411
412int sw_sync_timeline_create(void)
413{
Gustavo Padovanffc687b2016-06-10 16:51:29 -0300414 int ret;
415
416 ret = open("/sys/kernel/debug/sync/sw_sync", O_RDWR);
417 if (ret < 0)
418 ret = open("/dev/sw_sync", O_RDWR);
419
420 return ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800421}
422
423int sw_sync_timeline_inc(int fd, unsigned count)
424{
425 __u32 arg = count;
426
427 return ioctl(fd, SW_SYNC_IOC_INC, &arg);
428}
429
430int sw_sync_fence_create(int fd, const char *name, unsigned value)
431{
432 struct sw_sync_create_fence_data data;
433 int err;
434
435 data.value = value;
436 strlcpy(data.name, name, sizeof(data.name));
437
438 err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
439 if (err < 0)
440 return err;
441
442 return data.fence;
443}