sync.c revision ffc687baad033ecc96f6c560b205fea61afe9e41
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> 20#include <malloc.h> 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 28#include <sync/sync.h> 29 30 31struct sw_sync_create_fence_data { 32 __u32 value; 33 char name[32]; 34 __s32 fence; 35}; 36 37#define SW_SYNC_IOC_MAGIC 'W' 38#define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0, struct sw_sync_create_fence_data) 39#define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32) 40 41int sync_wait(int fd, int timeout) 42{ 43 __s32 to = timeout; 44 45 return ioctl(fd, SYNC_IOC_LEGACY_WAIT, &to); 46} 47 48int sync_merge(const char *name, int fd1, int fd2) 49{ 50 struct sync_legacy_merge_data data; 51 int err; 52 53 data.fd2 = fd2; 54 strlcpy(data.name, name, sizeof(data.name)); 55 56 err = ioctl(fd1, SYNC_IOC_LEGACY_MERGE, &data); 57 if (err < 0) 58 return err; 59 60 return data.fence; 61} 62 63struct sync_fence_info_data *sync_fence_info(int fd) 64{ 65 struct sync_fence_info_data *info; 66 int err; 67 68 info = malloc(4096); 69 if (info == NULL) 70 return NULL; 71 72 info->len = 4096; 73 err = ioctl(fd, SYNC_IOC_LEGACY_FENCE_INFO, info); 74 if (err < 0) { 75 free(info); 76 return NULL; 77 } 78 79 return info; 80} 81 82struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info, 83 struct sync_pt_info *itr) 84{ 85 if (itr == NULL) 86 itr = (struct sync_pt_info *) info->pt_info; 87 else 88 itr = (struct sync_pt_info *) ((__u8 *)itr + itr->len); 89 90 if ((__u8 *)itr - (__u8 *)info >= (int)info->len) 91 return NULL; 92 93 return itr; 94} 95 96void sync_fence_info_free(struct sync_fence_info_data *info) 97{ 98 free(info); 99} 100 101 102int sw_sync_timeline_create(void) 103{ 104 int ret; 105 106 ret = open("/sys/kernel/debug/sync/sw_sync", O_RDWR); 107 if (ret < 0) 108 ret = open("/dev/sw_sync", O_RDWR); 109 110 return ret; 111} 112 113int sw_sync_timeline_inc(int fd, unsigned count) 114{ 115 __u32 arg = count; 116 117 return ioctl(fd, SW_SYNC_IOC_INC, &arg); 118} 119 120int sw_sync_fence_create(int fd, const char *name, unsigned value) 121{ 122 struct sw_sync_create_fence_data data; 123 int err; 124 125 data.value = value; 126 strlcpy(data.name, name, sizeof(data.name)); 127 128 err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data); 129 if (err < 0) 130 return err; 131 132 return data.fence; 133} 134