• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #ifndef LIBTIMER_TIMER_DEFINES_H
13 #define LIBTIMER_TIMER_DEFINES_H
14 
15 #include <dlist.h>
16 #include <limits.h>
17 #include <tee_defines.h>
18 #include <ipclib.h>
19 #include "tee_msg_type.h"
20 #define TIMER_PATH    "hmtimer"
21 
22 #define TMR_OK  0
23 #define TMR_ERR 1
24 
25 #define TIMER_INDEX_RTC   0
26 #define TIMER_INDEX_TIMER 1
27 
28 #define IPC_NAME_MAX      32
29 #define MAX_NUM_OF_TIMERS 2
30 
31 #define TIMER_CREATE_SECONDS_THRESHOLD 2
32 #define MAX_SECONDS_PER_YEAR 31622400LL
33 #define NS_PER_SECONDS 1000000000L
34 #define NS_PER_MSEC    1000000
35 #define US_PER_SECONDS 1000000
36 #define MS_PER_SECONDS 1000
37 #define US_PER_MSEC    1000
38 #define NS_PER_USEC    1000
39 
40 #define TIMER_INV_VALUE   0
41 #define TIMEVAL_MAX       ((int64_t) ((~((uint64_t)1 << 63)) & (~((uint64_t)0xFFFFFFFF))))
42 #define UPPER_32_BITS(n) ((uint32_t)(((uint64_t)(n)) >> 32))
43 #define LOWER_32_BITS(n) ((uint32_t)(n))
44 
45 #define INVALID_SESSION_ID             0
46 
47 /* The timer event is Inactive */
48 #define TIMER_STATE_INACTIVE 0x00U
49 /* The timer event is active and is waiting for expiration */
50 #define TIMER_STATE_ACTIVE 0x01U
51 /* The timer is expired and is waiting on the callback list to be executed */
52 #define TIMER_STATE_PENDING 0x02U
53 /* The timer event is currently being executed */
54 #define TIMER_STATE_EXECUTING 0x04U
55 /* The timer event is currently being destroy */
56 #define TIMER_STATE_DESTROY 0x05U
57 
58 #define CMD_TIMER_GENERIC 0xDDEA
59 #define CMD_TIMER_RTC     0xDDEB
60 
61 #define EPOCH_YEAR      1970
62 #define DAYSPERLYEAR    366
63 #define DAYSPERNYEAR    365
64 
65 #define MINSPERHOUR     60
66 #define SECSPERMIN      60
67 #define SECSPERHOUR     (SECSPERMIN * MINSPERHOUR)
68 #define SECSPERDAY      (SECSPERHOUR * HOURSPERDAY)
69 
70 #define DAYSPERWEEK     7
71 #define HOURSPERDAY     24
72 
73 #define MONSPERYEAR     12
74 #define TIME_ZONE_EIGHT 8
75 
76 
77 #define TIMER_MSG_NUM_MAX            16
78 #define TIMER_RMSG_MAX_NUM           4
79 #define TIME_OUT_NEVER (-1)
80 
81 struct timer_req_msg_t {
82     msg_header header;
83     uint64_t args[TIMER_MSG_NUM_MAX];
84     cref_t job_handler;
85 } __attribute__((__packed__));
86 
87 struct timer_reply_msg_t {
88     msg_header header;
89     cref_t tcb_cref;
90     uint64_t regs[TIMER_RMSG_MAX_NUM];
91 } __attribute__((__packed__));
92 
93 #define TIMER_REQ_MSG_SIZE (sizeof(struct timer_req_msg_t))
94 #define TIMER_REP_MSG_SIZE (sizeof(struct timer_reply_msg_t))
95 
96 /*
97  * Some functions have been exported to other module use this struct as a parameter,
98  * so we cannot modify the typdef, and cannot delete the struct also.
99  */
100 typedef union {
101     int64_t tval64;
102     struct {
103         int32_t nsec;
104         int32_t sec;
105     } tval;
106 } timeval_t;
107 
108 /*
109  * The elements of this structure are useful
110  * for implementing the sw_timer
111  */
112 struct sw_timer_info {
113     timeval_t sw_timestamp;
114     uint64_t abs_cycles_count;
115     uint64_t cycles_count_old;
116     uint64_t cycles_count_new;
117     timeval_t timer_period;
118     timeval_t clock_period;
119 };
120 
121 enum timer_class_type {
122     /* timer event using timer10 */
123     TIMER_GENERIC,
124     /* timer event using RTC */
125     TIMER_RTC,
126     TIMER_CLASSIC,
127 };
128 
129 enum timer_callback_mode {
130     /* The handler function should be run in softirq */
131     TIMER_CALLBACK_SOFTIRQ,
132     /* The handler function should be run in hardirq context itself */
133     TIMER_CALLBACK_HARDIRQ,
134     /* The handler function should be executed in hardirq and it should not
135      * restart the timer */
136     TIMER_CALLBACK_HARDIRQ_NORESTART,
137     /* A special callback mode for timeout notification */
138     TIMER_CALLBACK_TIMEOUT
139 };
140 
141 typedef int32_t (*sw_timer_event_handler)(void *);
142 
143 struct sw_timer_event_hdl_info {
144     sw_timer_event_handler hdl;
145     void *priv_data;
146 };
147 
148 struct timer_clock_info {
149     struct timer_cpu_info *cpu_info;
150     int clock_id;
151     /* list for active timer event */
152     struct dlist_node active;
153     /* list for created timer event */
154     struct dlist_node avail;
155     timeval_t clock_period;
156     timeval_t timer_period;
157     int shift;
158     uint32_t mult;
159 };
160 
161 struct timer_cpu_info {
162     /* 0 for RTC, 1 for timer60 */
163     struct timer_clock_info clock_info[MAX_NUM_OF_TIMERS];
164     timeval_t expires_next[MAX_NUM_OF_TIMERS];
165 };
166 
167 struct timer_private_data_kernel {
168     uint32_t dev_id;
169     struct tee_uuid uuid;
170     uint32_t session_id;
171     uint32_t type;
172     uint32_t expire_time;
173 };
174 
175 typedef struct {
176     /* node for active timer event */
177     struct dlist_node node;
178     struct dlist_node callback_entry;
179     /* node for created timer event */
180     struct dlist_node c_node;
181     uint64_t handle;
182     timeval_t expires;
183     struct timer_clock_info *clk_info;
184     int32_t (*handler)(void *);
185     uint32_t state;
186     int callback_mode;
187     /* 0:timer60, 1:RTC */
188     int32_t timer_class;
189     struct timer_private_data_kernel timer_attr;
190     int32_t pid;
191     uint32_t app_handler;
192     cref_t timer_channel;
193     char path_name[IPC_NAME_MAX];
194     void *data;
195 } timer_event;
196 
197 struct timer_attr_data_kernel {
198     uint32_t type;
199     uint32_t timer_id;
200     uint32_t timer_class;
201     uint64_t handle;
202 };
203 
204 /*
205  * Some functions have been exported to other module use this struct as a parameter,
206  * so we cannot modify the typdef, and cannot delete the struct also.
207  */
208 typedef struct {
209     uint32_t dev_id;
210     struct tee_uuid uuid;
211     uint32_t session_id;
212     struct timer_attr_data_kernel property;
213     uint32_t expire_time;
214 } timer_notify_data_kernel;
215 
216 typedef struct tee_time_t {
217     int32_t seconds;
218     int32_t millis;
219 } tee_time_kernel;
220 
221 typedef struct tee_date_t {
222     int32_t seconds;
223     int32_t millis;
224     int32_t min;
225     int32_t hour;
226     int32_t day;
227     int32_t month;
228     int32_t year;
229 } tee_date_time_kernel;
230 
231 struct tee_time_stamp {
232     uint32_t seconds;
233     uint32_t nanos;
234 };
235 
236 void release_timer_event(const TEE_UUID *uuid);
237 #endif
238