2 * sync / sw_sync abstraction
3 * Copyright 2015-2016 Collabora Ltd.
5 * Based on the implementation from the Android Open Source Project,
7 * Copyright 2012 Google, Inc
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
35 #include <sys/ioctl.h>
37 #include <sys/types.h>
42 #include <linux/sync_file.h>
46 struct sw_sync_create_fence_data {
52 #define SW_SYNC_IOC_MAGIC 'W'
53 #define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\
54 struct sw_sync_create_fence_data)
55 #define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
58 int sync_wait(int fd, int timeout)
63 fds.events = POLLIN | POLLERR;
65 return poll(&fds, 1, timeout);
68 int sync_merge(const char *name, int fd1, int fd2)
70 struct sync_merge_data data = {};
74 strncpy(data.name, name, sizeof(data.name) - 1);
75 data.name[sizeof(data.name) - 1] = '\0';
77 err = ioctl(fd1, SYNC_IOC_MERGE, &data);
84 static struct sync_file_info *sync_file_info(int fd)
86 struct sync_file_info *info;
87 struct sync_fence_info *fence_info;
90 info = calloc(1, sizeof(*info));
94 err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
100 num_fences = info->num_fences;
104 info->num_fences = num_fences;
106 fence_info = calloc(num_fences, sizeof(*fence_info));
112 info->sync_fence_info = (uint64_t)(unsigned long)fence_info;
114 err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
125 static void sync_file_info_free(struct sync_file_info *info)
127 free((void *)(unsigned long)info->sync_fence_info);
131 int sync_fence_size(int fd)
134 struct sync_file_info *info = sync_file_info(fd);
139 count = info->num_fences;
141 sync_file_info_free(info);
146 int sync_fence_count_with_status(int fd, int status)
148 unsigned int i, count = 0;
149 struct sync_fence_info *fence_info = NULL;
150 struct sync_file_info *info = sync_file_info(fd);
155 fence_info = (struct sync_fence_info *)(unsigned long)info->sync_fence_info;
156 for (i = 0 ; i < info->num_fences ; i++) {
157 if (fence_info[i].status == status)
161 sync_file_info_free(info);
166 int sw_sync_timeline_create(void)
168 return open("/sys/kernel/debug/sync/sw_sync", O_RDWR);
171 int sw_sync_timeline_inc(int fd, unsigned int count)
175 return ioctl(fd, SW_SYNC_IOC_INC, &arg);
178 int sw_sync_timeline_is_valid(int fd)
185 status = fcntl(fd, F_GETFD, 0);
186 return (status >= 0);
189 void sw_sync_timeline_destroy(int fd)
191 if (sw_sync_timeline_is_valid(fd))
195 int sw_sync_fence_create(int fd, const char *name, unsigned int value)
197 struct sw_sync_create_fence_data data = {};
201 strncpy(data.name, name, sizeof(data.name) - 1);
202 data.name[sizeof(data.name) - 1] = '\0';
204 err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
211 int sw_sync_fence_is_valid(int fd)
214 return sw_sync_timeline_is_valid(fd);
217 void sw_sync_fence_destroy(int fd)
219 if (sw_sync_fence_is_valid(fd))