1 /**************************************************************************
5 Copyright 2012 - 2016 Samsung Electronics co., Ltd. All Rights Reserved.
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>
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:
20 The above copyright notice and this permission notice (including the
21 next paragraph) shall be included in all copies or substantial portions
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.
32 **************************************************************************/
34 #include "tbm_bufmgr_int.h"
39 #include <sys/ioctl.h>
40 #include <linux/types.h>
42 /* IOCTLs for timeline file object. */
43 #define TIMELINE_IOC_MAGIC 'W'
44 #define TIMELINE_IOC_CREATE_FENCE _IOWR(TIMELINE_IOC_MAGIC, 0, struct create_fence_data)
45 #define TIMELINE_IOC_INC _IOW(TIMELINE_IOC_MAGIC, 1, __u32)
47 /* IOCTLs for fence file object. */
48 #define FENCE_IOC_MAGIC '>'
49 #define FENCE_IOC_WAIT _IOW(FENCE_IOC_MAGIC, 0, __s32)
50 #define FENCE_IOC_MERGE _IOWR(FENCE_IOC_MAGIC, 1, struct sync_merge_data)
52 /* Path to the sync device file. */
53 #define SYNC_DEVICE_PATH "/dev/sw_sync"
55 /* Argument data structure for the timeline.create_fence ioctl. */
56 struct create_fence_data {
57 __u32 value; /* Pt value on the timeline for the fence (IN) */
58 char name[32]; /* Name of the fence object (IN) */
59 __s32 fence; /* File descriptor for the created fence (OUT) */
62 /* Argument data structure for the fence.merge ioctl. */
63 struct sync_merge_data {
64 __s32 fd2; /* fence to merged with the fence (IN) */
65 char name[32]; /* Name of the new fence object (IN) */
66 __s32 fence; /* File descriptor for the new fence (OUT) */
69 #define ERRNO_BUF_SIZE 256
74 /* calling strerror_r() might overwrite errno, so save it. */
76 char buf[ERRNO_BUF_SIZE];
78 if (strerror_r(errnum, buf, ERRNO_BUF_SIZE) == 0)
79 TBM_LOG_E("errno : %d(%s)\n", errnum, buf);
81 TBM_LOG_E("errno : %d()\n", errnum);
85 _copy_string(char *dst, const char *src, size_t size)
90 if (src == NULL || size == 1) {
96 if ((*dst++ = *src++) == '\0')
104 tbm_sync_timeline_create(void)
106 tbm_fd timeline = open(SYNC_DEVICE_PATH, O_RDWR | O_CLOEXEC);
115 tbm_sync_timeline_inc(tbm_fd timeline, unsigned int count)
119 if (ioctl(timeline, TIMELINE_IOC_INC, &arg) == -1) {
128 tbm_sync_fence_create(tbm_fd timeline, const char *name, unsigned int value)
130 struct create_fence_data data = { .value = value };
132 _copy_string(data.name, name, 32);
134 if (ioctl(timeline, TIMELINE_IOC_CREATE_FENCE, &data) == -1) {
143 tbm_sync_fence_wait(tbm_fd fence, int timeout)
148 /* Retry while ioctl() ended up with interrupt. */
150 ret = ioctl(fence, FENCE_IOC_WAIT, &arg);
151 } while (ret == -1 && errno == EINTR);
153 /* ioctl() was successful. */
157 /* ioctl() ended up with timeout. */
161 /* ioctl() failed for some reason. */
167 tbm_sync_fence_merge(const char *name, tbm_fd fence1, tbm_fd fence2)
169 struct sync_merge_data data = { .fd2 = fence2 };
171 _copy_string(data.name, name, 32);
173 if (ioctl(fence1, FENCE_IOC_MERGE, &data) == -1) {