blob: fff62e77c85d0e8eacb889fe2072d1427a1fe52c [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
19#include <fcntl.h>
Elliott Hughesa744b052015-01-28 11:37:57 -080020#include <malloc.h>
Erik Gilling196b3a52012-03-07 15:30:33 -080021#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 Ferrisf83c7922016-08-24 14:49:18 -070028#include <sync/sync.h>
29
Christopher Ferrisf83c7922016-08-24 14:49:18 -070030
Christopher Ferris1514bb42016-12-12 17:32:55 -080031struct 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
Erik Gilling984d3572012-08-21 18:21:18 -070041int sync_wait(int fd, int timeout)
Erik Gilling196b3a52012-03-07 15:30:33 -080042{
Erik Gilling984d3572012-08-21 18:21:18 -070043 __s32 to = timeout;
Erik Gilling196b3a52012-03-07 15:30:33 -080044
Gustavo Padovand6bbc5d2016-06-10 16:09:36 -030045 return ioctl(fd, SYNC_IOC_LEGACY_WAIT, &to);
Erik Gilling196b3a52012-03-07 15:30:33 -080046}
47
48int sync_merge(const char *name, int fd1, int fd2)
49{
Gustavo Padovand6bbc5d2016-06-10 16:09:36 -030050 struct sync_legacy_merge_data data;
Erik Gilling196b3a52012-03-07 15:30:33 -080051 int err;
52
53 data.fd2 = fd2;
54 strlcpy(data.name, name, sizeof(data.name));
55
Gustavo Padovand6bbc5d2016-06-10 16:09:36 -030056 err = ioctl(fd1, SYNC_IOC_LEGACY_MERGE, &data);
Erik Gilling196b3a52012-03-07 15:30:33 -080057 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;
Gustavo Padovand6bbc5d2016-06-10 16:09:36 -030073 err = ioctl(fd, SYNC_IOC_LEGACY_FENCE_INFO, info);
Erik Gilling196b3a52012-03-07 15:30:33 -080074 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 return open("/dev/sw_sync", O_RDWR);
105}
106
107int sw_sync_timeline_inc(int fd, unsigned count)
108{
109 __u32 arg = count;
110
111 return ioctl(fd, SW_SYNC_IOC_INC, &arg);
112}
113
114int sw_sync_fence_create(int fd, const char *name, unsigned value)
115{
116 struct sw_sync_create_fence_data data;
117 int err;
118
119 data.value = value;
120 strlcpy(data.name, name, sizeof(data.name));
121
122 err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
123 if (err < 0)
124 return err;
125
126 return data.fence;
127}