| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 19 | #include <errno.h> | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 20 | #include <fcntl.h> | 
| Elliott Hughes | a744b05 | 2015-01-28 11:37:57 -0800 | [diff] [blame] | 21 | #include <malloc.h> | 
| Jesse Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 22 | #include <poll.h> | 
|  | 23 | #include <stdatomic.h> | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 24 | #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 Hall | 41129a2 | 2017-02-13 15:19:24 -0800 | [diff] [blame] | 31 | #include <android/sync.h> | 
| Christopher Ferris | f83c792 | 2016-08-24 14:49:18 -0700 | [diff] [blame] | 32 |  | 
| Jesse Hall | 82d377b | 2018-06-13 11:52:10 -0700 | [diff] [blame] | 33 | /* 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 | */ | 
|  | 38 | struct sync_fence_info_data { | 
|  | 39 | uint32_t len; | 
|  | 40 | char name[32]; | 
|  | 41 | int32_t status; | 
|  | 42 | uint8_t pt_info[0]; | 
|  | 43 | }; | 
|  | 44 | struct 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 | }; | 
|  | 52 | struct sync_fence_info_data* sync_fence_info(int fd); | 
|  | 53 | struct sync_pt_info* sync_pt_info(struct sync_fence_info_data* info, struct sync_pt_info* itr); | 
|  | 54 | void sync_fence_info_free(struct sync_fence_info_data* info); | 
|  | 55 |  | 
| Jesse Hall | b7fdb2a | 2017-02-12 16:42:11 -0800 | [diff] [blame] | 56 | /* Legacy Sync API */ | 
|  | 57 |  | 
|  | 58 | struct 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 Ferris | f83c792 | 2016-08-24 14:49:18 -0700 | [diff] [blame] | 95 |  | 
| Christopher Ferris | 1514bb4 | 2016-12-12 17:32:55 -0800 | [diff] [blame] | 96 | struct 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 Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 106 | // --------------------------------------------------------------------------- | 
|  | 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 |  | 
|  | 115 | enum uapi_version { | 
|  | 116 | UAPI_UNKNOWN, | 
|  | 117 | UAPI_MODERN, | 
|  | 118 | UAPI_LEGACY | 
|  | 119 | }; | 
|  | 120 | static atomic_int g_uapi_version = ATOMIC_VAR_INIT(UAPI_UNKNOWN); | 
|  | 121 |  | 
|  | 122 | // --------------------------------------------------------------------------- | 
|  | 123 |  | 
| Erik Gilling | 984d357 | 2012-08-21 18:21:18 -0700 | [diff] [blame] | 124 | int sync_wait(int fd, int timeout) | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 125 | { | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 126 | struct pollfd fds; | 
|  | 127 | int ret; | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 128 |  | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 129 | 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 Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
| Jesse Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 154 | static int legacy_sync_merge(const char *name, int fd1, int fd2) | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 155 | { | 
| Jesse Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 156 | 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 |  | 
|  | 167 | static int modern_sync_merge(const char *name, int fd1, int fd2) | 
|  | 168 | { | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 169 | struct sync_merge_data data; | 
|  | 170 | int ret; | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 171 |  | 
|  | 172 | data.fd2 = fd2; | 
|  | 173 | strlcpy(data.name, name, sizeof(data.name)); | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 174 | data.flags = 0; | 
|  | 175 | data.pad = 0; | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 176 |  | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 177 | ret = ioctl(fd1, SYNC_IOC_MERGE, &data); | 
| Jesse Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 178 | if (ret < 0) | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 179 | return ret; | 
| Jesse Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 180 | return data.fence; | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | int 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 Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 199 | } | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 200 |  | 
| Jesse Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 201 | 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 Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 209 | static struct sync_fence_info_data *legacy_sync_fence_info(int fd) | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 210 | { | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 211 | struct sync_fence_info_data *legacy_info; | 
|  | 212 | struct sync_pt_info *legacy_pt_info; | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 213 | int err; | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 214 |  | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 215 | legacy_info = malloc(4096); | 
|  | 216 | if (legacy_info == NULL) | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 217 | return NULL; | 
|  | 218 |  | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 219 | legacy_info->len = 4096; | 
|  | 220 | err = ioctl(fd, SYNC_IOC_LEGACY_FENCE_INFO, legacy_info); | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 221 | if (err < 0) { | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 222 | free(legacy_info); | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 223 | return NULL; | 
|  | 224 | } | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 225 | return legacy_info; | 
|  | 226 | } | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 227 |  | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 228 | static 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 Shah | 90a7460 | 2017-08-01 13:54:21 -0700 | [diff] [blame] | 243 |  | 
|  | 244 | info->num_fences = local_info.num_fences; | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 245 | info->sync_fence_info = (__u64)(uintptr_t)(info + 1); | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 246 |  | 
|  | 247 | err = ioctl(fd, SYNC_IOC_FILE_INFO, info); | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 248 | if (err < 0) { | 
|  | 249 | free(info); | 
|  | 250 | return NULL; | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 253 | return info; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | static 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 Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 267 | legacy_info->len = sizeof(*legacy_info) + | 
| Jesse Hall | 077ffd5 | 2017-02-12 16:01:36 -0800 | [diff] [blame] | 268 | num_fences * sizeof(struct sync_pt_info); | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 269 | 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 Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 273 | for (uint32_t i = 0; i < num_fences; i++) { | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 274 | 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 Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 283 | return legacy_info; | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 284 | } | 
| Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 285 |  | 
| Jesse Hall | 8366616 | 2017-02-12 16:32:39 -0800 | [diff] [blame] | 286 | static 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 Hall | 8366616 | 2017-02-12 16:32:39 -0800 | [diff] [blame] | 303 | 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 Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 325 | struct sync_fence_info_data *sync_fence_info(int fd) | 
|  | 326 | { | 
|  | 327 | struct sync_fence_info_data *legacy_info; | 
| Jesse Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 328 | int uapi; | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 329 |  | 
| Jesse Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 330 | 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 Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 342 |  | 
|  | 343 | struct sync_file_info* file_info; | 
|  | 344 | file_info = modern_sync_file_info(fd); | 
|  | 345 | if (!file_info) | 
|  | 346 | return NULL; | 
| Jesse Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 347 | if (uapi == UAPI_UNKNOWN) { | 
|  | 348 | atomic_store_explicit(&g_uapi_version, UAPI_MODERN, | 
|  | 349 | memory_order_release); | 
|  | 350 | } | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 351 | legacy_info = sync_file_info_to_legacy_fence_info(file_info); | 
|  | 352 | sync_file_info_free(file_info); | 
|  | 353 | return legacy_info; | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 354 | } | 
|  | 355 |  | 
| Jesse Hall | 8366616 | 2017-02-12 16:32:39 -0800 | [diff] [blame] | 356 | struct sync_file_info* sync_file_info(int32_t fd) | 
|  | 357 | { | 
|  | 358 | struct sync_file_info *info; | 
| Jesse Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 359 | 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 Hall | 8366616 | 2017-02-12 16:32:39 -0800 | [diff] [blame] | 374 | struct sync_fence_info_data *legacy_info; | 
| Jesse Hall | 8366616 | 2017-02-12 16:32:39 -0800 | [diff] [blame] | 375 | legacy_info = legacy_sync_fence_info(fd); | 
|  | 376 | if (!legacy_info) | 
|  | 377 | return NULL; | 
| Jesse Hall | 6cd0fc5 | 2017-02-18 21:51:04 -0800 | [diff] [blame] | 378 | if (uapi == UAPI_UNKNOWN) { | 
|  | 379 | atomic_store_explicit(&g_uapi_version, UAPI_LEGACY, | 
|  | 380 | memory_order_release); | 
|  | 381 | } | 
| Jesse Hall | 8366616 | 2017-02-12 16:32:39 -0800 | [diff] [blame] | 382 | info = legacy_fence_info_to_sync_file_info(legacy_info); | 
|  | 383 | sync_fence_info_free(legacy_info); | 
|  | 384 | return info; | 
|  | 385 | } | 
|  | 386 |  | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 387 | struct 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 |  | 
|  | 401 | void sync_fence_info_free(struct sync_fence_info_data *info) | 
|  | 402 | { | 
|  | 403 | free(info); | 
|  | 404 | } | 
|  | 405 |  | 
| Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 406 | void sync_file_info_free(struct sync_file_info *info) | 
|  | 407 | { | 
|  | 408 | free(info); | 
|  | 409 | } | 
|  | 410 |  | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 411 |  | 
|  | 412 | int sw_sync_timeline_create(void) | 
|  | 413 | { | 
| Gustavo Padovan | ffc687b | 2016-06-10 16:51:29 -0300 | [diff] [blame] | 414 | 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 Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 421 | } | 
|  | 422 |  | 
|  | 423 | int 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 |  | 
|  | 430 | int 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 | } |