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