blob: 169dc3678e64a02c132d742d14b0210f6d304cea [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
Jamie Gennis3be33e42012-06-13 16:40:54 -070024#include <linux/sw_sync.h>
25
Erik Gilling196b3a52012-03-07 15:30:33 -080026#include <sys/ioctl.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29
Christopher Ferrisf83c7922016-08-24 14:49:18 -070030#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.
34struct 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 Gilling984d3572012-08-21 18:21:18 -070045int sync_wait(int fd, int timeout)
Erik Gilling196b3a52012-03-07 15:30:33 -080046{
Erik Gilling984d3572012-08-21 18:21:18 -070047 __s32 to = timeout;
Erik Gilling196b3a52012-03-07 15:30:33 -080048
49 return ioctl(fd, SYNC_IOC_WAIT, &to);
50}
51
52int 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
67struct 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
86struct 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
100void sync_fence_info_free(struct sync_fence_info_data *info)
101{
102 free(info);
103}
104
105
106int sw_sync_timeline_create(void)
107{
108 return open("/dev/sw_sync", O_RDWR);
109}
110
111int 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
118int 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}