49d026670f42027e52a6eb2b0fe1fc2799608a91
[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
44 /* IOCTLs for timeline file object. */
45 #define TIMELINE_IOC_MAGIC                      'W'
46 #define TIMELINE_IOC_CREATE_FENCE       _IOWR(TIMELINE_IOC_MAGIC, 0, struct create_fence_data)
47 #define TIMELINE_IOC_INC                        _IOW(TIMELINE_IOC_MAGIC, 1, __u32)
48
49 /* IOCTLs for fence file object. */
50 #define FENCE_IOC_MAGIC         '>'
51 #define FENCE_IOC_WAIT          _IOW(FENCE_IOC_MAGIC, 0, __s32)
52 #define FENCE_IOC_MERGE         _IOWR(FENCE_IOC_MAGIC, 1, struct sync_merge_data)
53
54 /* Path to the sync device legacy file. */
55 #define SYNC_DEVICE_PATH_LEGACY "/dev/sw_sync"
56
57 /* Path to the sync device file. */
58 #define SYNC_DEVICE_PATH        "/sys/kernel/debug/sync/sw_sync"
59
60 /* Argument data structure for the timeline.create_fence ioctl. */
61 struct create_fence_data {
62         __u32   value;          /* Pt value on the timeline for the fence (IN) */
63         char    name[32];       /* Name of the fence object (IN) */
64         __s32   fence;          /* File descriptor for the created fence (OUT) */
65 };
66
67 /* Argument data structure for the fence.merge ioctl. */
68 struct sync_merge_data {
69         __s32   fd2;            /* fence to merged with the fence (IN) */
70         char    name[32];       /* Name of the new fence object (IN) */
71         __s32   fence;          /* File descriptor for the new fence (OUT) */
72 };
73
74 #define ERRNO_BUF_SIZE  256
75
76 /* LCOV_EXCL_START */
77 static inline void
78 _log_errno()
79 {
80         /* calling strerror_r() might overwrite errno, so save it. */
81         int             errnum = errno;
82         char    buf[ERRNO_BUF_SIZE];
83
84         if (strerror_r(errnum, buf, ERRNO_BUF_SIZE) == 0) {
85                 TBM_ERR("errno : %d(%s)\n", errnum, buf);
86                 return;
87         } else {
88                 TBM_ERR("errno : %d()\n", errnum);
89                 return;
90         }
91 }
92
93 static inline void
94 _copy_string(char *dst, const char *src, size_t size)
95 {
96         if (size == 0)
97                 return;
98
99         if (src == NULL || size == 1) {
100                 *dst = '\0';
101                 return;
102         }
103
104         while (size-- != 1) {
105                 if ((*dst++ = *src++) == '\0')
106                         return;
107         }
108
109         *dst = '\0';
110 }
111
112 tbm_fd
113 tbm_sync_timeline_create(void)
114 {
115         tbm_fd timeline = open(SYNC_DEVICE_PATH, O_RDWR | O_CLOEXEC);
116
117         if (timeline < 0)
118                 timeline = open(SYNC_DEVICE_PATH_LEGACY, O_RDWR | O_CLOEXEC);
119
120         if (timeline == -1)
121                 _log_errno();
122
123         return timeline;
124 }
125
126 int
127 tbm_sync_timeline_inc(tbm_fd timeline, unsigned int count)
128 {
129         __u32 arg = count;
130
131         if (ioctl(timeline, TIMELINE_IOC_INC, &arg) == -1) {
132                 _log_errno();
133                 return 0;
134         }
135
136         return 1;
137 }
138
139 tbm_fd
140 tbm_sync_fence_create(tbm_fd timeline, const char *name, unsigned int value)
141 {
142         struct create_fence_data data = { .value = value };
143
144         _copy_string(data.name, name, 32);
145
146         if (ioctl(timeline, TIMELINE_IOC_CREATE_FENCE, &data) == -1) {
147                 _log_errno();
148                 return -1;
149         }
150
151         return data.fence;
152 }
153
154 int
155 tbm_sync_fence_wait(tbm_fd fence, int timeout)
156 {
157         __s32   arg = timeout;
158         int             ret;
159
160         /* Retry while ioctl() ended up with interrupt. */
161         do {
162                 ret = ioctl(fence, FENCE_IOC_WAIT, &arg);
163         } while (ret == -1 && errno == EINTR);
164
165         /* ioctl() was successful. */
166         if (ret == 0)
167                 return 1;
168
169         /* ioctl() ended up with timeout. */
170         if (errno == ETIME)
171                 return -1;
172
173         /* ioctl() failed for some reason. */
174         _log_errno();
175         return 0;
176 }
177
178 tbm_fd
179 tbm_sync_fence_merge(const char *name, tbm_fd fence1, tbm_fd fence2)
180 {
181         struct sync_merge_data data = { .fd2 = fence2 };
182
183         _copy_string(data.name, name, 32);
184
185         if (ioctl(fence1, FENCE_IOC_MERGE, &data) == -1) {
186                 _log_errno();
187                 return -1;
188         }
189
190         return data.fence;
191 }
192 /* LCOV_EXCL_STOP */