1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "fence.h"
17
18 #include <cerrno>
19 #include <fcntl.h>
20 #include <poll.h>
21 #include <sys/ioctl.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include <hilog/log.h>
27 #include <securec.h>
28 #include <linux/sync_file.h>
29
IsSupportSwSync()30 bool IsSupportSwSync()
31 {
32 struct stat syncStatus = {};
33 if (!stat("/sys/kernel/debug/sync/sw_sync", &syncStatus)) {
34 return true;
35 }
36 return false;
37 }
38
CreateTimeline()39 int CreateTimeline()
40 {
41 int timeline = open("/sys/kernel/debug/sync/sw_sync", O_RDWR);
42 if (timeline < 0) {
43 HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "the timeline is invalid");
44 }
45 return timeline;
46 }
47
CreateFenceFromTimeline(int timeline,const char * name,unsigned int totalSteps)48 int CreateFenceFromTimeline(int timeline, const char* name, unsigned int totalSteps)
49 {
50 struct sw_sync_create_fence_data {
51 unsigned int value;
52 char name[32];
53 unsigned int fence;
54 };
55
56 struct sw_sync_create_fence_data data = {
57 .value = totalSteps
58 };
59
60 if (strcpy_s(data.name, sizeof(data.name), name)) {
61 HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "Create Fence From Timeline Failed");
62 return -1;
63 }
64 int ret = ioctl(timeline, _IOWR('W', 0, struct sw_sync_create_fence_data), &data);
65 if (ret != 0) {
66 HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", " data.fence are invalid");
67 return -1;
68 }
69 return data.fence;
70 }
71
FenceHold(int fd,int timeout)72 int FenceHold(int fd, int timeout)
73 {
74 int ret = 0;
75 if (fd < 0) {
76 HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "the fd is invalid");
77 return -1;
78 }
79
80 struct pollfd pollfds = {
81 .fd = fd,
82 .events = POLLIN | POLLERR,
83 };
84
85 do {
86 ret = poll(&pollfds, 1, timeout);
87 } while (ret == -1 && errno == EINTR);
88 return ret;
89 }
90
TimelineActivate(int timeline,unsigned int step)91 int TimelineActivate(int timeline, unsigned int step)
92 {
93 int ret = ioctl(timeline, _IOW('W', 1, unsigned int), &step);
94 if (ret != 0) {
95 return errno;
96 }
97 return ret;
98 }
99
FenceGetStatus(int fd)100 enum FenceStatus FenceGetStatus(int fd)
101 {
102 int ret = FenceHold(fd, 0);
103
104 enum FenceStatus status = ACTIVE;
105 if (ret < 0) {
106 status = ERROR;
107 } else if (ret > 0) {
108 status = SIGNALED;
109 }
110 return status;
111 }
112
FenceMerge(const char * name,int fd1,int fd2)113 int FenceMerge(const char* name, int fd1, int fd2)
114 {
115 int result_code;
116 struct sync_merge_data sync_merge_data = {};
117 if (strcpy_s(sync_merge_data.name, sizeof(sync_merge_data.name), name)) {
118 HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "FenceMerge strcpy name failed");
119 return -1;
120 }
121
122 if (fd1 >= 0 && fd2 < 0) {
123 sync_merge_data.fd2 = fd1;
124 result_code = ioctl(fd1, SYNC_IOC_MERGE, &sync_merge_data);
125 }
126
127 if (fd1 < 0 && fd2 >= 0) {
128 sync_merge_data.fd2 = fd2;
129 result_code = ioctl(fd2, SYNC_IOC_MERGE, &sync_merge_data);
130 }
131
132 if (fd1 >= 0 && fd2 >= 0) {
133 sync_merge_data.fd2 = fd2;
134 result_code = ioctl(fd1, SYNC_IOC_MERGE, &sync_merge_data);
135 }
136
137 if (fd1 < 0 && fd2 < 0) {
138 HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "fd1 and fd2 are invalid");
139 return -1;
140 }
141
142 if (result_code < 0) {
143 HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "merge failed %{public}d", errno);
144 return result_code;
145 }
146 return sync_merge_data.fence;
147 }
148
149