| 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> | 
 | 23 |  | 
| Jamie Gennis | 3be33e4 | 2012-06-13 16:40:54 -0700 | [diff] [blame] | 24 | #include <linux/sw_sync.h> | 
 | 25 |  | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 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 |  | 
 | 32 | // The sync code is undergoing a major change. Add enough in to get | 
 | 33 | // everything to compile wih the latest uapi headers. | 
 | 34 | struct sync_merge_data { | 
 | 35 |   int32_t fd2; | 
 | 36 |   char name[32]; | 
 | 37 |   int32_t fence; | 
 | 38 | }; | 
 | 39 |  | 
 | 40 | #define SYNC_IOC_MAGIC '>' | 
 | 41 | #define SYNC_IOC_WAIT _IOW(SYNC_IOC_MAGIC, 0, __s32) | 
 | 42 | #define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 1, struct sync_merge_data) | 
 | 43 | #define SYNC_IOC_FENCE_INFO _IOWR(SYNC_IOC_MAGIC, 2, struct sync_fence_info_data) | 
 | 44 |  | 
| Erik Gilling | 984d357 | 2012-08-21 18:21:18 -0700 | [diff] [blame] | 45 | int sync_wait(int fd, int timeout) | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 46 | { | 
| Erik Gilling | 984d357 | 2012-08-21 18:21:18 -0700 | [diff] [blame] | 47 |     __s32 to = timeout; | 
| Erik Gilling | 196b3a5 | 2012-03-07 15:30:33 -0800 | [diff] [blame] | 48 |  | 
 | 49 |     return ioctl(fd, SYNC_IOC_WAIT, &to); | 
 | 50 | } | 
 | 51 |  | 
 | 52 | int sync_merge(const char *name, int fd1, int fd2) | 
 | 53 | { | 
 | 54 |     struct sync_merge_data data; | 
 | 55 |     int err; | 
 | 56 |  | 
 | 57 |     data.fd2 = fd2; | 
 | 58 |     strlcpy(data.name, name, sizeof(data.name)); | 
 | 59 |  | 
 | 60 |     err = ioctl(fd1, SYNC_IOC_MERGE, &data); | 
 | 61 |     if (err < 0) | 
 | 62 |         return err; | 
 | 63 |  | 
 | 64 |     return data.fence; | 
 | 65 | } | 
 | 66 |  | 
 | 67 | struct sync_fence_info_data *sync_fence_info(int fd) | 
 | 68 | { | 
 | 69 |     struct sync_fence_info_data *info; | 
 | 70 |     int err; | 
 | 71 |  | 
 | 72 |     info = malloc(4096); | 
 | 73 |     if (info == NULL) | 
 | 74 |         return NULL; | 
 | 75 |  | 
 | 76 |     info->len = 4096; | 
 | 77 |     err = ioctl(fd, SYNC_IOC_FENCE_INFO, info); | 
 | 78 |     if (err < 0) { | 
 | 79 |         free(info); | 
 | 80 |         return NULL; | 
 | 81 |     } | 
 | 82 |  | 
 | 83 |     return info; | 
 | 84 | } | 
 | 85 |  | 
 | 86 | struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info, | 
 | 87 |                                   struct sync_pt_info *itr) | 
 | 88 | { | 
 | 89 |     if (itr == NULL) | 
 | 90 |         itr = (struct sync_pt_info *) info->pt_info; | 
 | 91 |     else | 
 | 92 |         itr = (struct sync_pt_info *) ((__u8 *)itr + itr->len); | 
 | 93 |  | 
 | 94 |     if ((__u8 *)itr - (__u8 *)info >= (int)info->len) | 
 | 95 |         return NULL; | 
 | 96 |  | 
 | 97 |     return itr; | 
 | 98 | } | 
 | 99 |  | 
 | 100 | void sync_fence_info_free(struct sync_fence_info_data *info) | 
 | 101 | { | 
 | 102 |     free(info); | 
 | 103 | } | 
 | 104 |  | 
 | 105 |  | 
 | 106 | int sw_sync_timeline_create(void) | 
 | 107 | { | 
 | 108 |     return open("/dev/sw_sync", O_RDWR); | 
 | 109 | } | 
 | 110 |  | 
 | 111 | int sw_sync_timeline_inc(int fd, unsigned count) | 
 | 112 | { | 
 | 113 |     __u32 arg = count; | 
 | 114 |  | 
 | 115 |     return ioctl(fd, SW_SYNC_IOC_INC, &arg); | 
 | 116 | } | 
 | 117 |  | 
 | 118 | int sw_sync_fence_create(int fd, const char *name, unsigned value) | 
 | 119 | { | 
 | 120 |     struct sw_sync_create_fence_data data; | 
 | 121 |     int err; | 
 | 122 |  | 
 | 123 |     data.value = value; | 
 | 124 |     strlcpy(data.name, name, sizeof(data.name)); | 
 | 125 |  | 
 | 126 |     err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data); | 
 | 127 |     if (err < 0) | 
 | 128 |         return err; | 
 | 129 |  | 
 | 130 |     return data.fence; | 
 | 131 | } |