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 | |
| 19 | #include <fcntl.h> |
Elliott Hughes | a744b05 | 2015-01-28 11:37:57 -0800 | [diff] [blame] | 20 | #include <malloc.h> |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <string.h> |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <poll.h> |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 25 | |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | |
Christopher Ferris | f83c792 | 2016-08-24 14:49:18 -0700 | [diff] [blame] | 30 | #include <sync/sync.h> |
| 31 | |
Christopher Ferris | f83c792 | 2016-08-24 14:49:18 -0700 | [diff] [blame] | 32 | |
Christopher Ferris | 1514bb4 | 2016-12-12 17:32:55 -0800 | [diff] [blame] | 33 | struct 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 Gilling | 984d357 | 2012-08-21 18:21:18 -0700 | [diff] [blame] | 43 | int sync_wait(int fd, int timeout) |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 44 | { |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 45 | struct pollfd fds; |
| 46 | int ret; |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 47 | |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 48 | 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 Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | int sync_merge(const char *name, int fd1, int fd2) |
| 74 | { |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 75 | struct sync_legacy_merge_data legacy_data; |
| 76 | struct sync_merge_data data; |
| 77 | int ret; |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 78 | |
| 79 | data.fd2 = fd2; |
| 80 | strlcpy(data.name, name, sizeof(data.name)); |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 81 | data.flags = 0; |
| 82 | data.pad = 0; |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 83 | |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 84 | 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 Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 97 | |
| 98 | return data.fence; |
| 99 | } |
| 100 | |
Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 101 | static struct sync_fence_info_data *legacy_sync_fence_info(int fd) |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 102 | { |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 103 | struct sync_fence_info_data *legacy_info; |
| 104 | struct sync_pt_info *legacy_pt_info; |
Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 105 | int err; |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 106 | |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 107 | legacy_info = malloc(4096); |
| 108 | if (legacy_info == NULL) |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 109 | return NULL; |
| 110 | |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 111 | legacy_info->len = 4096; |
| 112 | err = ioctl(fd, SYNC_IOC_LEGACY_FENCE_INFO, legacy_info); |
Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 113 | if (err < 0) { |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 114 | free(legacy_info); |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 115 | return NULL; |
| 116 | } |
Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 117 | return legacy_info; |
| 118 | } |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 119 | |
Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 120 | static 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 Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 136 | |
| 137 | err = ioctl(fd, SYNC_IOC_FILE_INFO, info); |
Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 138 | if (err < 0) { |
| 139 | free(info); |
| 140 | return NULL; |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 141 | } |
| 142 | |
Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 143 | return info; |
| 144 | } |
| 145 | |
| 146 | static 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 Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 157 | legacy_info->len = sizeof(*legacy_info) + |
Jesse Hall | 077ffd5 | 2017-02-12 16:01:36 -0800 | [diff] [blame] | 158 | num_fences * sizeof(struct sync_pt_info); |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 159 | 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 Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 163 | for (uint32_t i = 0; i < num_fences; i++) { |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 164 | 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 Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 173 | return legacy_info; |
Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 174 | } |
Gustavo Padovan | 61ab0d7 | 2016-06-11 11:11:19 -0300 | [diff] [blame] | 175 | |
Jesse Hall | 8366616 | 2017-02-12 16:32:39 -0800 | [diff] [blame^] | 176 | static 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 Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 216 | struct 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 Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Jesse Hall | 8366616 | 2017-02-12 16:32:39 -0800 | [diff] [blame^] | 233 | struct 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 Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 250 | struct 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 | |
| 264 | void sync_fence_info_free(struct sync_fence_info_data *info) |
| 265 | { |
| 266 | free(info); |
| 267 | } |
| 268 | |
Jesse Hall | 8953082 | 2017-02-12 16:17:22 -0800 | [diff] [blame] | 269 | void sync_file_info_free(struct sync_file_info *info) |
| 270 | { |
| 271 | free(info); |
| 272 | } |
| 273 | |
Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 274 | |
| 275 | int sw_sync_timeline_create(void) |
| 276 | { |
Gustavo Padovan | ffc687b | 2016-06-10 16:51:29 -0300 | [diff] [blame] | 277 | 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 Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | int 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 | |
| 293 | int 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 | } |