blob: baeccda472735714fb6abc7a3675654573492a6d [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 Hallb7fdb2a2017-02-12 16:42:11 -080033/* Legacy Sync API */
34
35struct sync_legacy_merge_data {
36 int32_t fd2;
37 char name[32];
38 int32_t fence;
39};
40
41/**
42 * DOC: SYNC_IOC_MERGE - merge two fences
43 *
44 * Takes a struct sync_merge_data. Creates a new fence containing copies of
45 * the sync_pts in both the calling fd and sync_merge_data.fd2. Returns the
46 * new fence's fd in sync_merge_data.fence
47 *
48 * This is the legacy version of the Sync API before the de-stage that happened
49 * on Linux kernel 4.7.
50 */
51#define SYNC_IOC_LEGACY_MERGE _IOWR(SYNC_IOC_MAGIC, 1, \
52 struct sync_legacy_merge_data)
53
54/**
55 * DOC: SYNC_IOC_LEGACY_FENCE_INFO - get detailed information on a fence
56 *
57 * Takes a struct sync_fence_info_data with extra space allocated for pt_info.
58 * Caller should write the size of the buffer into len. On return, len is
59 * updated to reflect the total size of the sync_fence_info_data including
60 * pt_info.
61 *
62 * pt_info is a buffer containing sync_pt_infos for every sync_pt in the fence.
63 * To iterate over the sync_pt_infos, use the sync_pt_info.len field.
64 *
65 * This is the legacy version of the Sync API before the de-stage that happened
66 * on Linux kernel 4.7.
67 */
68#define SYNC_IOC_LEGACY_FENCE_INFO _IOWR(SYNC_IOC_MAGIC, 2,\
69 struct sync_fence_info_data)
70
71/* SW Sync API */
Christopher Ferrisf83c7922016-08-24 14:49:18 -070072
Christopher Ferris1514bb42016-12-12 17:32:55 -080073struct sw_sync_create_fence_data {
74 __u32 value;
75 char name[32];
76 __s32 fence;
77};
78
79#define SW_SYNC_IOC_MAGIC 'W'
80#define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0, struct sw_sync_create_fence_data)
81#define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
82
Jesse Hall6cd0fc52017-02-18 21:51:04 -080083// ---------------------------------------------------------------------------
84// Support for caching the sync uapi version.
85//
86// This library supports both legacy (android/staging) uapi and modern
87// (mainline) sync uapi. Library calls first try one uapi, and if that fails,
88// try the other. Since any given kernel only supports one uapi version, after
89// the first successful syscall we know what the kernel supports and can skip
90// trying the other.
91
92enum uapi_version {
93 UAPI_UNKNOWN,
94 UAPI_MODERN,
95 UAPI_LEGACY
96};
97static atomic_int g_uapi_version = ATOMIC_VAR_INIT(UAPI_UNKNOWN);
98
99// ---------------------------------------------------------------------------
100
Erik Gilling984d3572012-08-21 18:21:18 -0700101int sync_wait(int fd, int timeout)
Erik Gilling196b3a52012-03-07 15:30:33 -0800102{
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300103 struct pollfd fds;
104 int ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800105
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300106 if (fd < 0) {
107 errno = EINVAL;
108 return -1;
109 }
110
111 fds.fd = fd;
112 fds.events = POLLIN;
113
114 do {
115 ret = poll(&fds, 1, timeout);
116 if (ret > 0) {
117 if (fds.revents & (POLLERR | POLLNVAL)) {
118 errno = EINVAL;
119 return -1;
120 }
121 return 0;
122 } else if (ret == 0) {
123 errno = ETIME;
124 return -1;
125 }
126 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
127
128 return ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800129}
130
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800131static int legacy_sync_merge(const char *name, int fd1, int fd2)
Erik Gilling196b3a52012-03-07 15:30:33 -0800132{
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800133 struct sync_legacy_merge_data data;
134 int ret;
135
136 data.fd2 = fd2;
137 strlcpy(data.name, name, sizeof(data.name));
138 ret = ioctl(fd1, SYNC_IOC_LEGACY_MERGE, &data);
139 if (ret < 0)
140 return ret;
141 return data.fence;
142}
143
144static int modern_sync_merge(const char *name, int fd1, int fd2)
145{
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300146 struct sync_merge_data data;
147 int ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800148
149 data.fd2 = fd2;
150 strlcpy(data.name, name, sizeof(data.name));
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300151 data.flags = 0;
152 data.pad = 0;
Erik Gilling196b3a52012-03-07 15:30:33 -0800153
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300154 ret = ioctl(fd1, SYNC_IOC_MERGE, &data);
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800155 if (ret < 0)
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300156 return ret;
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800157 return data.fence;
158}
159
160int sync_merge(const char *name, int fd1, int fd2)
161{
162 int uapi;
163 int ret;
164
165 uapi = atomic_load_explicit(&g_uapi_version, memory_order_acquire);
166
167 if (uapi == UAPI_MODERN || uapi == UAPI_UNKNOWN) {
168 ret = modern_sync_merge(name, fd1, fd2);
169 if (ret >= 0 || errno != ENOTTY) {
170 if (ret >= 0 && uapi == UAPI_UNKNOWN) {
171 atomic_store_explicit(&g_uapi_version, UAPI_MODERN,
172 memory_order_release);
173 }
174 return ret;
175 }
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300176 }
Erik Gilling196b3a52012-03-07 15:30:33 -0800177
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800178 ret = legacy_sync_merge(name, fd1, fd2);
179 if (ret >= 0 && uapi == UAPI_UNKNOWN) {
180 atomic_store_explicit(&g_uapi_version, UAPI_LEGACY,
181 memory_order_release);
182 }
183 return ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800184}
185
Jesse Hall89530822017-02-12 16:17:22 -0800186static struct sync_fence_info_data *legacy_sync_fence_info(int fd)
Erik Gilling196b3a52012-03-07 15:30:33 -0800187{
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300188 struct sync_fence_info_data *legacy_info;
189 struct sync_pt_info *legacy_pt_info;
Jesse Hall89530822017-02-12 16:17:22 -0800190 int err;
Erik Gilling196b3a52012-03-07 15:30:33 -0800191
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300192 legacy_info = malloc(4096);
193 if (legacy_info == NULL)
Erik Gilling196b3a52012-03-07 15:30:33 -0800194 return NULL;
195
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300196 legacy_info->len = 4096;
197 err = ioctl(fd, SYNC_IOC_LEGACY_FENCE_INFO, legacy_info);
Jesse Hall89530822017-02-12 16:17:22 -0800198 if (err < 0) {
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300199 free(legacy_info);
Erik Gilling196b3a52012-03-07 15:30:33 -0800200 return NULL;
201 }
Jesse Hall89530822017-02-12 16:17:22 -0800202 return legacy_info;
203}
Erik Gilling196b3a52012-03-07 15:30:33 -0800204
Jesse Hall89530822017-02-12 16:17:22 -0800205static struct sync_file_info *modern_sync_file_info(int fd)
206{
207 struct sync_file_info local_info;
208 struct sync_file_info *info;
209 int err;
210
211 memset(&local_info, 0, sizeof(local_info));
212 err = ioctl(fd, SYNC_IOC_FILE_INFO, &local_info);
213 if (err < 0)
214 return NULL;
215
216 info = calloc(1, sizeof(struct sync_file_info) +
217 local_info.num_fences * sizeof(struct sync_fence_info));
218 if (!info)
219 return NULL;
220 info->sync_fence_info = (__u64)(uintptr_t)(info + 1);
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300221
222 err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
Jesse Hall89530822017-02-12 16:17:22 -0800223 if (err < 0) {
224 free(info);
225 return NULL;
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300226 }
227
Jesse Hall89530822017-02-12 16:17:22 -0800228 return info;
229}
230
231static struct sync_fence_info_data *sync_file_info_to_legacy_fence_info(
232 const struct sync_file_info *info)
233{
234 struct sync_fence_info_data *legacy_info;
235 struct sync_pt_info *legacy_pt_info;
236 const struct sync_fence_info *fence_info = sync_get_fence_info(info);
237 const uint32_t num_fences = info->num_fences;
238
239 legacy_info = malloc(4096);
240 if (legacy_info == NULL)
241 return NULL;
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300242 legacy_info->len = sizeof(*legacy_info) +
Jesse Hall077ffd52017-02-12 16:01:36 -0800243 num_fences * sizeof(struct sync_pt_info);
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300244 strlcpy(legacy_info->name, info->name, sizeof(legacy_info->name));
245 legacy_info->status = info->status;
246
247 legacy_pt_info = (struct sync_pt_info *)legacy_info->pt_info;
Jesse Hall89530822017-02-12 16:17:22 -0800248 for (uint32_t i = 0; i < num_fences; i++) {
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300249 legacy_pt_info[i].len = sizeof(*legacy_pt_info);
250 strlcpy(legacy_pt_info[i].obj_name, fence_info[i].obj_name,
251 sizeof(legacy_pt_info->obj_name));
252 strlcpy(legacy_pt_info[i].driver_name, fence_info[i].driver_name,
253 sizeof(legacy_pt_info->driver_name));
254 legacy_pt_info[i].status = fence_info[i].status;
255 legacy_pt_info[i].timestamp_ns = fence_info[i].timestamp_ns;
256 }
257
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300258 return legacy_info;
Jesse Hall89530822017-02-12 16:17:22 -0800259}
Gustavo Padovan61ab0d72016-06-11 11:11:19 -0300260
Jesse Hall83666162017-02-12 16:32:39 -0800261static struct sync_file_info* legacy_fence_info_to_sync_file_info(
262 struct sync_fence_info_data *legacy_info)
263{
264 struct sync_file_info *info;
265 struct sync_pt_info *pt;
266 struct sync_fence_info *fence;
267 size_t num_fences;
268 int err;
269
270 pt = NULL;
271 num_fences = 0;
272 while ((pt = sync_pt_info(legacy_info, pt)) != NULL)
273 num_fences++;
274
275 info = calloc(1, sizeof(struct sync_file_info) +
276 num_fences * sizeof(struct sync_fence_info));
277 if (!info) {
278 free(legacy_info);
279 return NULL;
280 }
281 info->sync_fence_info = (__u64)(uintptr_t)(info + 1);
282
283 strlcpy(info->name, legacy_info->name, sizeof(info->name));
284 info->status = legacy_info->status;
285 info->num_fences = num_fences;
286
287 pt = NULL;
288 fence = sync_get_fence_info(info);
289 while ((pt = sync_pt_info(legacy_info, pt)) != NULL) {
290 strlcpy(fence->obj_name, pt->obj_name, sizeof(fence->obj_name));
291 strlcpy(fence->driver_name, pt->driver_name,
292 sizeof(fence->driver_name));
293 fence->status = pt->status;
294 fence->timestamp_ns = pt->timestamp_ns;
295 fence++;
296 }
297
298 return info;
299}
300
Jesse Hall89530822017-02-12 16:17:22 -0800301struct sync_fence_info_data *sync_fence_info(int fd)
302{
303 struct sync_fence_info_data *legacy_info;
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800304 int uapi;
Jesse Hall89530822017-02-12 16:17:22 -0800305
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800306 uapi = atomic_load_explicit(&g_uapi_version, memory_order_acquire);
307
308 if (uapi == UAPI_LEGACY || uapi == UAPI_UNKNOWN) {
309 legacy_info = legacy_sync_fence_info(fd);
310 if (legacy_info || errno != ENOTTY) {
311 if (legacy_info && uapi == UAPI_UNKNOWN) {
312 atomic_store_explicit(&g_uapi_version, UAPI_LEGACY,
313 memory_order_release);
314 }
315 return legacy_info;
316 }
317 }
Jesse Hall89530822017-02-12 16:17:22 -0800318
319 struct sync_file_info* file_info;
320 file_info = modern_sync_file_info(fd);
321 if (!file_info)
322 return NULL;
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800323 if (uapi == UAPI_UNKNOWN) {
324 atomic_store_explicit(&g_uapi_version, UAPI_MODERN,
325 memory_order_release);
326 }
Jesse Hall89530822017-02-12 16:17:22 -0800327 legacy_info = sync_file_info_to_legacy_fence_info(file_info);
328 sync_file_info_free(file_info);
329 return legacy_info;
Erik Gilling196b3a52012-03-07 15:30:33 -0800330}
331
Jesse Hall83666162017-02-12 16:32:39 -0800332struct sync_file_info* sync_file_info(int32_t fd)
333{
334 struct sync_file_info *info;
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800335 int uapi;
336
337 uapi = atomic_load_explicit(&g_uapi_version, memory_order_acquire);
338
339 if (uapi == UAPI_MODERN || uapi == UAPI_UNKNOWN) {
340 info = modern_sync_file_info(fd);
341 if (info || errno != ENOTTY) {
342 if (info && uapi == UAPI_UNKNOWN) {
343 atomic_store_explicit(&g_uapi_version, UAPI_MODERN,
344 memory_order_release);
345 }
346 return info;
347 }
348 }
349
Jesse Hall83666162017-02-12 16:32:39 -0800350 struct sync_fence_info_data *legacy_info;
Jesse Hall83666162017-02-12 16:32:39 -0800351 legacy_info = legacy_sync_fence_info(fd);
352 if (!legacy_info)
353 return NULL;
Jesse Hall6cd0fc52017-02-18 21:51:04 -0800354 if (uapi == UAPI_UNKNOWN) {
355 atomic_store_explicit(&g_uapi_version, UAPI_LEGACY,
356 memory_order_release);
357 }
Jesse Hall83666162017-02-12 16:32:39 -0800358 info = legacy_fence_info_to_sync_file_info(legacy_info);
359 sync_fence_info_free(legacy_info);
360 return info;
361}
362
Erik Gilling196b3a52012-03-07 15:30:33 -0800363struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info,
364 struct sync_pt_info *itr)
365{
366 if (itr == NULL)
367 itr = (struct sync_pt_info *) info->pt_info;
368 else
369 itr = (struct sync_pt_info *) ((__u8 *)itr + itr->len);
370
371 if ((__u8 *)itr - (__u8 *)info >= (int)info->len)
372 return NULL;
373
374 return itr;
375}
376
377void sync_fence_info_free(struct sync_fence_info_data *info)
378{
379 free(info);
380}
381
Jesse Hall89530822017-02-12 16:17:22 -0800382void sync_file_info_free(struct sync_file_info *info)
383{
384 free(info);
385}
386
Erik Gilling196b3a52012-03-07 15:30:33 -0800387
388int sw_sync_timeline_create(void)
389{
Gustavo Padovanffc687b2016-06-10 16:51:29 -0300390 int ret;
391
392 ret = open("/sys/kernel/debug/sync/sw_sync", O_RDWR);
393 if (ret < 0)
394 ret = open("/dev/sw_sync", O_RDWR);
395
396 return ret;
Erik Gilling196b3a52012-03-07 15:30:33 -0800397}
398
399int sw_sync_timeline_inc(int fd, unsigned count)
400{
401 __u32 arg = count;
402
403 return ioctl(fd, SW_SYNC_IOC_INC, &arg);
404}
405
406int sw_sync_fence_create(int fd, const char *name, unsigned value)
407{
408 struct sw_sync_create_fence_data data;
409 int err;
410
411 data.value = value;
412 strlcpy(data.name, name, sizeof(data.name));
413
414 err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
415 if (err < 0)
416 return err;
417
418 return data.fence;
419}