remove getenv() function.
[platform/core/uifw/libtbm.git] / src / tbm_sync.c
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2012 - 2016 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>,
8                  Changyeon Lee <cyeon.lee@samsung.com>,
9                  Boram Park <boram1288.park@samsung.com>,
10                  Sangjin Lee <lsj119@samsung.com>
11
12 Permission is hereby granted, free of charge, to any person obtaining a
13 copy of this software and associated documentation files (the
14 "Software"), to deal in the Software without restriction, including
15 without limitation the rights to use, copy, modify, merge, publish,
16 distribute, sub license, and/or sell copies of the Software, and to
17 permit persons to whom the Software is furnished to do so, subject to
18 the following conditions:
19
20 The above copyright notice and this permission notice (including the
21 next paragraph) shall be included in all copies or substantial portions
22 of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
27 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
28 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
32 **************************************************************************/
33
34 #include "config.h"
35
36 #include "tbm_bufmgr_int.h"
37 #include "tbm_sync.h"
38
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/ioctl.h>
42 #include <linux/types.h>
43 #include <poll.h>
44
45 /* IOCTLs for timeline file object. */
46 #define TIMELINE_IOC_MAGIC                      'W'
47 #define TIMELINE_IOC_CREATE_FENCE       _IOWR(TIMELINE_IOC_MAGIC, 0, struct create_fence_data)
48 #define TIMELINE_IOC_INC                        _IOW(TIMELINE_IOC_MAGIC, 1, __u32)
49
50 /* IOCTLs for fence file object. */
51 #define FENCE_IOC_MAGIC                         '>'
52 #define FENCE_IOC_MERGE                         _IOWR(FENCE_IOC_MAGIC, 3, struct sync_merge_data)
53
54 #define FENCE_IOC_LEGACY_WAIT           _IOW(FENCE_IOC_MAGIC, 0, __s32)
55 #define FENCE_IOC_LEGACY_MERGE          _IOWR(FENCE_IOC_MAGIC, 1, struct sync_legacy_merge_data)
56
57 /* Path to the sync device legacy file. */
58 #define SYNC_DEVICE_PATH_LEGACY "/dev/sw_sync"
59
60 /* Path to the sync device file. */
61 #define SYNC_DEVICE_PATH        "/sys/kernel/debug/sync/sw_sync"
62
63 /* Argument data structure for the timeline.create_fence ioctl. */
64 struct create_fence_data {
65         __u32   value;          /* Pt value on the timeline for the fence (IN) */
66         char    name[32];       /* Name of the fence object (IN) */
67         __s32   fence;          /* File descriptor for the created fence (OUT) */
68 };
69
70 /* Argument data structure for the fence.merge ioctl. */
71 struct sync_merge_data {
72         char    name[32];       /* Name of the new fence object (IN) */
73         __s32   fd2;            /* fence to merged with the fence (IN) */
74         __s32   fence;          /* File descriptor for the new fence (OUT) */
75         __u32   flags;          /* merge_data flags (IN) */
76         __u32   pad;            /* padding for 64-bit alignment, should always be zero */
77 };
78
79 /* Argument data structure for the legacy fence.merge ioctl. */
80 struct sync_legacy_merge_data {
81         __s32   fd2;            /* fence to merged with the fence (IN) */
82         char    name[32];       /* Name of the new fence object (IN) */
83         __s32   fence;          /* File descriptor for the new fence (OUT) */
84 };
85
86 #define ERRNO_BUF_SIZE  256
87
88 /* LCOV_EXCL_START */
89 static inline void
90 _log_errno()
91 {
92         /* calling strerror_r() might overwrite errno, so save it. */
93         int             errnum = errno;
94         char    buf[ERRNO_BUF_SIZE];
95
96         if (strerror_r(errnum, buf, ERRNO_BUF_SIZE) == 0) {
97                 TBM_ERR("errno : %d(%s)\n", errnum, buf);
98                 return;
99         } else {
100                 TBM_ERR("errno : %d()\n", errnum);
101                 return;
102         }
103 }
104
105 static inline void
106 _copy_string(char *dst, const char *src, size_t size)
107 {
108         if (size == 0)
109                 return;
110
111         if (src == NULL || size == 1) {
112                 *dst = '\0';
113                 return;
114         }
115
116         while (size-- != 1) {
117                 if ((*dst++ = *src++) == '\0')
118                         return;
119         }
120
121         *dst = '\0';
122 }
123
124 tbm_fd
125 tbm_sync_timeline_create(void)
126 {
127         tbm_fd timeline = open(SYNC_DEVICE_PATH, O_RDWR | O_CLOEXEC);
128
129         if (timeline < 0)
130                 timeline = open(SYNC_DEVICE_PATH_LEGACY, O_RDWR | O_CLOEXEC);
131
132         if (timeline == -1)
133                 _log_errno();
134
135         return timeline;
136 }
137
138 int
139 tbm_sync_timeline_inc(tbm_fd timeline, unsigned int count)
140 {
141         __u32 arg = count;
142
143         if (ioctl(timeline, TIMELINE_IOC_INC, &arg) == -1) {
144                 _log_errno();
145                 return 0;
146         }
147
148         return 1;
149 }
150
151 tbm_fd
152 tbm_sync_fence_create(tbm_fd timeline, const char *name, unsigned int value)
153 {
154         struct create_fence_data data = { .value = value };
155
156         _copy_string(data.name, name, 32);
157
158         if (ioctl(timeline, TIMELINE_IOC_CREATE_FENCE, &data) == -1) {
159                 _log_errno();
160                 return -1;
161         }
162
163         return data.fence;
164 }
165
166 int
167 tbm_sync_fence_wait(tbm_fd fence, int timeout)
168 {
169         struct pollfd fds;
170         int ret;
171
172         fds.fd = fence;
173         fds.events = POLLIN;
174
175         do {
176                 ret = poll(&fds, 1, timeout);
177                 if (ret > 0) {
178                         if (fds.revents & (POLLERR | POLLNVAL)) {
179                                 errno = EINVAL;
180                                 _log_errno();
181                                 return 0;
182                         }
183
184                         return 1;
185                 } else if (ret == 0) {
186                         errno = ETIME;
187                         _log_errno();
188                         return 0;
189                 }
190         } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
191
192         _log_errno();
193         return 0;
194 }
195
196 tbm_fd
197 tbm_sync_fence_merge(const char *name, tbm_fd fence1, tbm_fd fence2)
198 {
199         struct sync_merge_data data = { .fd2 = fence2 };
200         int ret;
201
202         _copy_string(data.name, name, 32);
203
204         ret = ioctl(fence1, FENCE_IOC_MERGE, &data);
205         if (ret < 0 && errno == ENOTTY) {
206                 struct sync_legacy_merge_data legacy_data = {. fd2 = fence2 };
207
208                 _copy_string(legacy_data.name, name, 32);
209
210                 ret = ioctl(fence1, FENCE_IOC_LEGACY_MERGE, &legacy_data);
211                 if (ret < 0) {
212                         _log_errno();
213                         return -1;
214                 }
215         } else if (ret < 0) {
216                 _log_errno();
217                 return -1;
218         }
219
220         return data.fence;
221 }
222 /* LCOV_EXCL_STOP */