17ec681f3Smrg/* 27ec681f3Smrg * sync abstraction 37ec681f3Smrg * Copyright 2015-2016 Collabora Ltd. 47ec681f3Smrg * 57ec681f3Smrg * Based on the implementation from the Android Open Source Project, 67ec681f3Smrg * 77ec681f3Smrg * Copyright 2012 Google, Inc 87ec681f3Smrg * 97ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 107ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 117ec681f3Smrg * to deal in the Software without restriction, including without limitation 127ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 137ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 147ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 157ec681f3Smrg * 167ec681f3Smrg * The above copyright notice and this permission notice shall be included in 177ec681f3Smrg * all copies or substantial portions of the Software. 187ec681f3Smrg * 197ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 207ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 217ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 227ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 237ec681f3Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 247ec681f3Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 257ec681f3Smrg * OTHER DEALINGS IN THE SOFTWARE. 267ec681f3Smrg */ 277ec681f3Smrg 287ec681f3Smrg#ifndef _LIBSYNC_H 297ec681f3Smrg#define _LIBSYNC_H 307ec681f3Smrg 317ec681f3Smrg#include <assert.h> 327ec681f3Smrg#include <errno.h> 337ec681f3Smrg#include <stdbool.h> 347ec681f3Smrg#include <stdint.h> 357ec681f3Smrg#include <string.h> 367ec681f3Smrg#include <sys/ioctl.h> 377ec681f3Smrg#include <sys/poll.h> 387ec681f3Smrg#include <unistd.h> 397ec681f3Smrg 407ec681f3Smrg#if defined(__cplusplus) 417ec681f3Smrgextern "C" { 427ec681f3Smrg#endif 437ec681f3Smrg 447ec681f3Smrg#ifdef ANDROID 457ec681f3Smrg/* On Android, rely on the system's libsync instead of rolling our own 467ec681f3Smrg * sync_wait() and sync_merge(). This gives us compatibility with pre-4.7 477ec681f3Smrg * Android kernels. 487ec681f3Smrg */ 497ec681f3Smrg#include <android/sync.h> 507ec681f3Smrg 517ec681f3Smrg/** 527ec681f3Smrg * Check if the fd represents a valid fence-fd. 537ec681f3Smrg * 547ec681f3Smrg * The android variant of this debug helper is implemented on top of the 557ec681f3Smrg * system's libsync for compatibility with pre-4.7 android kernels. 567ec681f3Smrg */ 577ec681f3Smrgstatic inline bool 587ec681f3Smrgsync_valid_fd(int fd) 597ec681f3Smrg{ 607ec681f3Smrg /* sync_file_info() only available in SDK 26. */ 617ec681f3Smrg#if ANDROID_API_LEVEL >= 26 627ec681f3Smrg struct sync_file_info *info = sync_file_info(fd); 637ec681f3Smrg if (!info) 647ec681f3Smrg return false; 657ec681f3Smrg sync_file_info_free(info); 667ec681f3Smrg#endif 677ec681f3Smrg return true; 687ec681f3Smrg} 697ec681f3Smrg#else 707ec681f3Smrg 717ec681f3Smrg#ifndef SYNC_IOC_MERGE 727ec681f3Smrg/* duplicated from linux/sync_file.h to avoid build-time dependency 737ec681f3Smrg * on new (v4.7) kernel headers. Once distro's are mostly using 747ec681f3Smrg * something newer than v4.7 drop this and #include <linux/sync_file.h> 757ec681f3Smrg * instead. 767ec681f3Smrg */ 777ec681f3Smrgstruct sync_merge_data { 787ec681f3Smrg char name[32]; 797ec681f3Smrg int32_t fd2; 807ec681f3Smrg int32_t fence; 817ec681f3Smrg uint32_t flags; 827ec681f3Smrg uint32_t pad; 837ec681f3Smrg}; 847ec681f3Smrg 857ec681f3Smrgstruct sync_file_info { 867ec681f3Smrg char name[32]; 877ec681f3Smrg int32_t status; 887ec681f3Smrg uint32_t flags; 897ec681f3Smrg uint32_t num_fences; 907ec681f3Smrg uint32_t pad; 917ec681f3Smrg 927ec681f3Smrg uint64_t sync_fence_info; 937ec681f3Smrg}; 947ec681f3Smrg 957ec681f3Smrg#define SYNC_IOC_MAGIC '>' 967ec681f3Smrg#define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data) 977ec681f3Smrg#define SYNC_IOC_FILE_INFO _IOWR(SYNC_IOC_MAGIC, 4, struct sync_file_info) 987ec681f3Smrg#endif 997ec681f3Smrg 1007ec681f3Smrg 1017ec681f3Smrgstatic inline int sync_wait(int fd, int timeout) 1027ec681f3Smrg{ 1037ec681f3Smrg struct pollfd fds = {0}; 1047ec681f3Smrg int ret; 1057ec681f3Smrg 1067ec681f3Smrg fds.fd = fd; 1077ec681f3Smrg fds.events = POLLIN; 1087ec681f3Smrg 1097ec681f3Smrg do { 1107ec681f3Smrg ret = poll(&fds, 1, timeout); 1117ec681f3Smrg if (ret > 0) { 1127ec681f3Smrg if (fds.revents & (POLLERR | POLLNVAL)) { 1137ec681f3Smrg errno = EINVAL; 1147ec681f3Smrg return -1; 1157ec681f3Smrg } 1167ec681f3Smrg return 0; 1177ec681f3Smrg } else if (ret == 0) { 1187ec681f3Smrg errno = ETIME; 1197ec681f3Smrg return -1; 1207ec681f3Smrg } 1217ec681f3Smrg } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); 1227ec681f3Smrg 1237ec681f3Smrg return ret; 1247ec681f3Smrg} 1257ec681f3Smrg 1267ec681f3Smrgstatic inline int sync_merge(const char *name, int fd1, int fd2) 1277ec681f3Smrg{ 1287ec681f3Smrg struct sync_merge_data data = {0}; 1297ec681f3Smrg int ret; 1307ec681f3Smrg 1317ec681f3Smrg data.fd2 = fd2; 1327ec681f3Smrg strncpy(data.name, name, sizeof(data.name)); 1337ec681f3Smrg 1347ec681f3Smrg do { 1357ec681f3Smrg ret = ioctl(fd1, SYNC_IOC_MERGE, &data); 1367ec681f3Smrg } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); 1377ec681f3Smrg 1387ec681f3Smrg if (ret < 0) 1397ec681f3Smrg return ret; 1407ec681f3Smrg 1417ec681f3Smrg return data.fence; 1427ec681f3Smrg} 1437ec681f3Smrg 1447ec681f3Smrg/** 1457ec681f3Smrg * Check if the fd represents a valid fence-fd. 1467ec681f3Smrg */ 1477ec681f3Smrgstatic inline bool 1487ec681f3Smrgsync_valid_fd(int fd) 1497ec681f3Smrg{ 1507ec681f3Smrg struct sync_file_info info = {{0}}; 1517ec681f3Smrg return ioctl(fd, SYNC_IOC_FILE_INFO, &info) >= 0; 1527ec681f3Smrg} 1537ec681f3Smrg 1547ec681f3Smrg#endif /* !ANDROID */ 1557ec681f3Smrg 1567ec681f3Smrg/* accumulate fd2 into fd1. If *fd1 is not a valid fd then dup fd2, 1577ec681f3Smrg * otherwise sync_merge() and close the old *fd1. This can be used 1587ec681f3Smrg * to implement the pattern: 1597ec681f3Smrg * 1607ec681f3Smrg * init() 1617ec681f3Smrg * { 1627ec681f3Smrg * batch.fence_fd = -1; 1637ec681f3Smrg * } 1647ec681f3Smrg * 1657ec681f3Smrg * // does *NOT* take ownership of fd 1667ec681f3Smrg * server_sync(int fd) 1677ec681f3Smrg * { 1687ec681f3Smrg * if (sync_accumulate("foo", &batch.fence_fd, fd)) { 1697ec681f3Smrg * ... error ... 1707ec681f3Smrg * } 1717ec681f3Smrg * } 1727ec681f3Smrg */ 1737ec681f3Smrgstatic inline int sync_accumulate(const char *name, int *fd1, int fd2) 1747ec681f3Smrg{ 1757ec681f3Smrg int ret; 1767ec681f3Smrg 1777ec681f3Smrg assert(fd2 >= 0); 1787ec681f3Smrg 1797ec681f3Smrg if (*fd1 < 0) { 1807ec681f3Smrg *fd1 = dup(fd2); 1817ec681f3Smrg return 0; 1827ec681f3Smrg } 1837ec681f3Smrg 1847ec681f3Smrg ret = sync_merge(name, *fd1, fd2); 1857ec681f3Smrg if (ret < 0) { 1867ec681f3Smrg /* leave *fd1 as it is */ 1877ec681f3Smrg return ret; 1887ec681f3Smrg } 1897ec681f3Smrg 1907ec681f3Smrg close(*fd1); 1917ec681f3Smrg *fd1 = ret; 1927ec681f3Smrg 1937ec681f3Smrg return 0; 1947ec681f3Smrg} 1957ec681f3Smrg 1967ec681f3Smrg#if defined(__cplusplus) 1977ec681f3Smrg} 1987ec681f3Smrg#endif 1997ec681f3Smrg 2007ec681f3Smrg#endif 201