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