1 /*
2  * Copyright (c) 2023 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 #ifndef QOS_INTERFACE_H
17 #define QOS_INTERFACE_H
18 #include "internal_inc/config.h"
19 #include "eu/worker_thread.h"
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /*
25  * generic
26  */
27 #define SYSTEM_UID 1000
28 #define ROOT_UID 0
29 
30 /*
31  * auth_ctrl
32  */
33 struct AuthCtrlData {
34     unsigned int uid;
35     unsigned int type;
36     unsigned int rtgUaFlag;
37     unsigned int qosUaFlag;
38     unsigned int status;
39 };
40 
41 enum AuthManipulateType {
42     AUTH_ENABLE = 1,
43     AUTH_DELETE,
44     AUTH_GET,
45     AUTH_SWITCH,
46     AUTH_MAX_NR,
47 };
48 
49 enum AuthStatus {
50     AUTH_STATUS_DISABLED = 1,
51     AUTH_STATUS_SYSTEM_SERVER = 2,
52     AUTH_STATUS_FOREGROUND = 3,
53     AUTH_STATUS_BACKGROUND = 4,
54     AUTH_STATUS_DEAD,
55 };
56 
57 enum AuthCtrlCmdid {
58     BASIC_AUTH_CTRL = 1,
59     AUTH_CTRL_MAX_NR
60 };
61 
62 #define AUTH_CTRL_IPC_MAGIG 0xCD
63 
64 #define BASIC_AUTH_CTRL_OPERATION \
65     _IOWR(AUTH_CTRL_IPC_MAGIG, BASIC_AUTH_CTRL, struct AuthCtrlData)
66 
67 /*
68  * qos ctrl
69  */
70 constexpr unsigned char QOS_NUM_MAX = 10;
71 
72 constexpr unsigned char AF_QOS_ALL = 0x0003;
73 constexpr unsigned char AF_QOS_DELEGATED = 0x0001;
74 
75 enum QosManipulateType {
76     QOS_APPLY = 1,
77     QOS_LEAVE,
78     QOS_GET,
79     QOS_MAX_NR,
80 };
81 
82 struct QosCtrlData {
83     int pid;
84     unsigned int type;
85     unsigned int level;
86     int qos;
87     int staticQos;
88     int dynamicQos;
89     int tagSchedEnable = false;
90 };
91 
92 struct QosPolicyData {
93     int latency_nice;
94     int uclamp_min;
95     int uclamp_max;
96     unsigned long affinity;
97     unsigned char priority;
98     unsigned char init_load;
99     unsigned char prefer_idle;
100 };
101 
102 constexpr unsigned char THREAD_CTRL_NUM = 4;
103 
104 struct ThreadAttrCtrl {
105     int tid;
106     bool prioritySetEnable;
107     bool affinitySetEnable;
108 };
109 
110 struct ThreadAttrCtrlDatas {
111     struct ThreadAttrCtrl ctrls[THREAD_CTRL_NUM];
112 };
113 
114 enum QosPolicyType {
115     QOS_POLICY_DEFAULT = 1,
116     QOS_POLICY_SYSTEM_SERVER = 2,
117     QOS_POLICY_FRONT = 3,
118     QOS_POLICY_BACK = 4,
119     QOS_POLICY_MAX_NR,
120 };
121 
122 constexpr unsigned char QOS_FLAG_NICE = 0X01;
123 constexpr unsigned char QOS_FLAG_LATENCY_NICE = 0X02;
124 constexpr unsigned char QOS_FLAG_UCLAMP = 0x04;
125 constexpr unsigned char QOS_FLAG_RT = 0x08;
126 
127 #define QOS_FLAG_ALL    (QOS_FLAG_NICE          | \
128             QOS_FLAG_LATENCY_NICE       | \
129             QOS_FLAG_UCLAMP     | \
130             QOS_FLAG_RT)
131 
132 struct QosPolicyDatas {
133     int policyType;
134     unsigned int policyFlag;
135     struct QosPolicyData policys[NR_QOS + 1];
136 };
137 
138 enum QosCtrlCmdid {
139     QOS_CTRL = 1,
140     QOS_POLICY,
141     QOS_THREAD_CTRL,
142     QOS_CTRL_MAX_NR
143 };
144 
145 #define QOS_CTRL_IPC_MAGIG 0xCC
146 
147 #define QOS_CTRL_BASIC_OPERATION \
148     _IOWR(QOS_CTRL_IPC_MAGIG, QOS_CTRL, struct QosCtrlData)
149 #define QOS_CTRL_POLICY_OPERATION \
150     _IOWR(QOS_CTRL_IPC_MAGIG, QOS_POLICY, struct QosPolicyDatas)
151 #define QOS_THREAD_CTRL_OPERATION \
152     _IOWR(QOS_CTRL_IPC_MAGIG, QOS_THREAD_CTRL, struct ThreadAttrCtrl)
153 
154 /*
155  * RTG
156  */
157 #define AF_RTG_ALL          0x1fff
158 #define AF_RTG_DELEGATED    0x1fff
159 
160 struct RtgEnableData {
161     int enable;
162     size_t len;
163     char *data;
164 };
165 
166 enum RtgSchedCmdid {
167     SET_ENABLE = 1,
168     SET_RTG,
169     SET_CONFIG,
170     SET_RTG_ATTR,
171     BEGIN_FRAME_FREQ = 5,
172     END_FRAME_FREQ,
173     END_SCENE,
174     SET_MIN_UTIL,
175     SET_MARGIN,
176     LIST_RTG = 10,
177     LIST_RTG_THREAD,
178     SEARCH_RTG,
179     GET_ENABLE,
180     RTG_CTRL_MAX_NR,
181 };
182 
183 #define RTG_SCHED_IPC_MAGIC 0xAB
184 
185 #define CMD_ID_SET_ENABLE \
186     _IOWR(RTG_SCHED_IPC_MAGIC, SET_ENABLE, struct RtgEnableData)
187 
188 /*
189  * interface
190  */
191 int FFRTEnableRtg(bool flag);
192 int FFRTAuthEnable(unsigned int uid, unsigned int uaFlag, unsigned int status);
193 int FFRTAuthPause(unsigned int uid);
194 int FFRTAuthDelete(unsigned int uid);
195 int FFRTAuthGet(unsigned int uid, unsigned int *uaFlag, unsigned int *status);
196 int FFRTAuthSwitch(unsigned int uid, unsigned int rtgFlag, unsigned int qosFlag, unsigned int status);
197 int FFRTQosApply(unsigned int level);
198 int FFRTQosApplyForOther(unsigned int level, int tid);
199 int FFRTQosLeave(void);
200 int FFRTQosLeaveForOther(int tid);
201 int FFRTQosGet(struct QosCtrlData &data);
202 int FFRTQosGetForOther(int tid, struct QosCtrlData &data);
203 int QosPolicy(struct QosPolicyDatas *policyDatas);
204 int ThreadCtrl(int tid, struct ThreadAttrCtrl &ctrlDatas);
205 typedef int (*Func_affinity)(unsigned long affinity, int tid);
206 void setFuncAffinity(Func_affinity func);
207 Func_affinity getFuncAffinity(void);
208 typedef void (*Func_priority)(unsigned char priority, ffrt::WorkerThread* thread);
209 void setFuncPriority(Func_priority func);
210 Func_priority getFuncPriority(void);
211 
212 #ifdef __cplusplus
213 }
214 #endif
215 
216 #endif /* OQS_INTERFACE_H */
217