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", "open file failed");
44         return -1;
45     }
46 
47     if (fcntl(timeline, F_GETFD, 0) < 0) {
48         HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "the timeline is valid");
49         close(timeline);
50         return -1;
51     }
52     return timeline;
53 }
54 
CreateFenceFromTimeline(int timeline,const char * name,unsigned int totalSteps)55 int CreateFenceFromTimeline(int timeline, const char* name, unsigned int totalSteps)
56 {
57     struct sw_sync_create_fence_data {
58         unsigned int value;
59         char name[32];
60         unsigned int fence;
61     };
62 
63     struct sw_sync_create_fence_data data = {
64         .value = totalSteps
65     };
66 
67     if (strcpy_s(data.name, sizeof(data.name), name)) {
68         HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "Create Fence From Timeline Failed");
69         return -1;
70     }
71     int ret = ioctl(timeline, _IOWR('W', 0, struct sw_sync_create_fence_data), &data);
72     if (ret != 0) {
73         HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", " data.fence are invalid");
74         return -1;
75     }
76     return data.fence;
77 }
78 
FenceHold(int fd,int timeout)79 int FenceHold(int fd, int timeout)
80 {
81     int ret = 0;
82     if (fd < 0) {
83         HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "the fd is invalid");
84         return -1;
85     }
86 
87     struct pollfd pollfds = {
88         .fd = fd,
89         .events = POLLIN | POLLERR,
90     };
91 
92     do {
93         ret = poll(&pollfds, 1, timeout);
94     } while (ret == -1 && errno == EINTR);
95     return ret;
96 }
97 
TimelineActivate(int timeline,unsigned int step)98 int TimelineActivate(int timeline, unsigned int step)
99 {
100     int ret = ioctl(timeline, _IOW('W', 1, unsigned int), &step);
101     if (ret != 0) {
102         return errno;
103     }
104     return ret;
105 }
106 
FenceGetStatus(int fd)107 enum FenceStatus FenceGetStatus(int fd)
108 {
109     int ret = FenceHold(fd, 0);
110 
111     enum FenceStatus status = ACTIVE;
112     if (ret < 0) {
113         status = ERROR;
114     } else if (ret > 0) {
115         status = SIGNALED;
116     }
117     return status;
118 }
119 
FenceMerge(const char * name,int fd1,int fd2)120 int FenceMerge(const char* name, int fd1, int fd2)
121 {
122     int result_code;
123     struct sync_merge_data sync_merge_data = {};
124     if (strcpy_s(sync_merge_data.name, sizeof(sync_merge_data.name), name)) {
125         HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "FenceMerge strcpy name failed");
126         return -1;
127     }
128 
129     if (fd1 >= 0 && fd2 < 0) {
130         sync_merge_data.fd2 = fd1;
131         result_code = ioctl(fd1, SYNC_IOC_MERGE, &sync_merge_data);
132     }
133 
134     if (fd1 < 0 && fd2 >= 0) {
135         sync_merge_data.fd2 = fd2;
136         result_code = ioctl(fd2, SYNC_IOC_MERGE, &sync_merge_data);
137     }
138 
139     if (fd1 >= 0 && fd2 >= 0) {
140         sync_merge_data.fd2 = fd2;
141         result_code = ioctl(fd1, SYNC_IOC_MERGE, &sync_merge_data);
142     }
143 
144     if (fd1 < 0 && fd2 < 0) {
145         HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "fd1 and fd2 are invalid");
146         return -1;
147     }
148 
149     if (result_code < 0) {
150         HiLogPrint(LOG_CORE, LOG_ERROR, 0, "fence", "merge failed %{public}d", errno);
151         return result_code;
152     }
153     return sync_merge_data.fence;
154 }
155 
156