1 /* 2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 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 * @defgroup hr_swtmr High-resolution timer 17 * @ingroup linux 18 */ 19 20 #ifndef _LINUX_HRTIMER_H 21 #define _LINUX_HRTIMER_H 22 23 #include "sys/types.h" 24 #include "los_base.h" 25 #include "los_task.h" 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif /* __cplusplus */ 30 31 /** 32 * @ingroup hr_swtmr 33 * Define the number of timer cycles in 1us. 34 */ 35 #define HRTIMER_PERUS (HRTIMER_CLOCK / 1000000.0) 36 37 /** 38 * @ingroup hr_swtmr 39 * Define s64 as a signed long integer. 40 */ 41 typedef signed long long s64; 42 43 /** 44 * @ingroup hr_swtmr 45 * Define s32 as a signed long integer. 46 */ 47 typedef signed int s32; 48 49 /** 50 * @ingroup hr_swtmr 51 * Define u64 as an unsigned long integer. 52 */ 53 typedef unsigned long long u64; 54 55 /** 56 * @ingroup hr_swtmr 57 * Structure of the scheduled time. 58 */ 59 union ktime { 60 s64 tv64; /**< Scheduled time for 64-bit CPU systems. */ 61 struct { 62 s32 sec, usec; 63 } tv; /**< Scheduled time for 32-bit CPU systems. */ 64 }; 65 66 /** 67 * @ingroup hr_swtmr 68 * Structure of a node in a high-resolution timer queue. 69 */ 70 struct timerqueue_node { 71 unsigned int node; /**< Not in use temporarily. */ 72 }; 73 74 /** 75 * @ingroup hr_swtmr 76 * Enumerative structure of the high-resolution timer mode arguments. 77 */ 78 enum hrtimer_mode { 79 HRTIMER_MODE_ABS = 0x0, /**< Time value is absolute. */ 80 HRTIMER_MODE_REL = 0x1, /**< Time value is relative to now. */ 81 HRTIMER_MODE_PINNED = 0x2, /**< Timer is bound to CPU. */ 82 }; 83 84 /** 85 * @ingroup hr_swtmr 86 * Enumerative structure of the return type of a high-resolution timer timeout callback function. 87 */ 88 enum hrtimer_restart { 89 HRTIMER_NORESTART, /**< The timer will not be restarted.*/ 90 HRTIMER_RESTART /**< The timer must be restarted.*/ 91 }; 92 struct hrtimer; 93 94 /** 95 * @ingroup hr_swtmr 96 * Define the function handler type of a high-resolution timer timeout callback function. 97 */ 98 typedef enum hrtimer_restart (*Handler)(struct hrtimer *); 99 100 /** 101 * @ingroup hr_swtmr 102 * Structure of parameters of a high-resolution timer API. 103 */ 104 struct hrtimer { 105 union ktime _softexpires; /**< Structure of the scheduled time. */ 106 Handler function; /**< Timeout callback function. */ 107 unsigned long state; /**< Timer working state. Not in use temporarily. */ 108 #ifdef CONFIG_TIMER_STATS 109 int start_pid; /**< ID of the task that invokes a high-resolution timer. 110 Not in use temporarily. */ 111 void *start_site; /**< Function that invokes a high-resolution timer. Not in use temporarily. */ 112 #define START_TASK_NAME_LEN 16 113 char start_comm[START_TASK_NAME_LEN]; /**< Name of the task that invokes a high-resolution timer. 114 Not in use temporarily. */ 115 #endif 116 }; 117 118 /** 119 * @ingroup hr_swtmr 120 * Parameter structure of the nodes of a high-resolution timer timeout callback function. 121 */ 122 struct handler_list_node { 123 struct handler_list_node *pstNext; /**< Pointer to the next node. */ 124 Handler pfnHandler; /**< Timeout callback function. */ 125 union ktime _softexpires; /**< Structure of the scheduled time. */ 126 }; 127 128 /** 129 * @ingroup hr_swtmr 130 * Parameter structure of a high-resolution timer node. 131 */ 132 struct hrtimer_list_node { 133 struct hrtimer_list_node *pstNext; /**< Pointer to the next node. */ 134 struct handler_list_node *HandlerHead; /**< Pointer to the node queue of a timeout callback function. */ 135 unsigned int set_time_reload; /**< Count of timers. */ 136 union ktime _softexpires; /**< Structure of the scheduled time. */ 137 }; 138 139 /** 140 * @ingroup hr_swtmr 141 * @brief Initialize a high-resolution timer. 142 * 143 * @par Description: 144 * This API is used to initialize a high-resolution timer to the given clock. 145 * @attention 146 * <ul> 147 * <li>The pointer to the high-resolution timer structure to be initialized must not be null.</li> 148 * </ul> 149 * 150 * @param timer [IN] Pointer to the high-resolution timer structure. 151 * @param clockId [IN] This parameter is not supported, so users can pass in any integer. 152 * @param mode [IN] Mode setting is currently not supported. 153 * 154 * @retval None. 155 * @par Dependency: 156 * <ul> 157 * <li>hrtimer.h: the header file that contains the API declaration.</li> 158 * </ul> 159 * @see None. 160 */ 161 void hrtimer_init(struct hrtimer *timer, clockid_t clockId, enum hrtimer_mode mode); 162 163 /** 164 * @ingroup hr_swtmr 165 * @brief Create a high-resolution timer. 166 * 167 * @par Description: 168 * This API is used to create a high-resolution timer node and initialize timer parameters. 169 * @attention 170 * <ul> 171 * <li>The passed-in pointer to the high-resolution timer structure must not be null.</li> 172 * <li>The value of the scheduled time cannot be zero.</li> 173 * </ul> 174 * 175 * @param timer [IN] Pointer to the high-resolution timer structure. 176 * @param time [IN] Structure of the scheduled time. 177 * @param handler [IN] Pointer to the timeout callback function. 178 * 179 * @retval -1 The high-resolution timer fails to be created because the pointer to 180 * the high-resolution timer structure is null. 181 * @retval 0 The high-resolution timer is successfully created. 182 * @par Dependency: 183 * <ul> 184 * <li>hrtimer.h: the header file that contains the API declaration.</li> 185 * </ul> 186 * @see None. 187 */ 188 int hrtimer_create(struct hrtimer *timer, union ktime time, Handler handler); 189 190 /** 191 * @ingroup hr_swtmr 192 * @brief Start a high-resolution timer. 193 * 194 * @par Description: 195 * This API is used to add a high-resolution timer node to the global linked list and start timing. 196 * @attention 197 * <ul> 198 * <li>The passed-in pointer to the high-resolution timer structure must not be null.</li> 199 * <li>The value of the scheduled time cannot be zero.</li> 200 * <li>The return value is inconsistent with the Linux standard interface definition.</li> 201 * </ul> 202 * 203 * @param timer [IN] Pointer to the high-resolution timer structure. 204 * @param time [IN] Structure of the scheduled time. 205 * @param mode [IN] Mode setting is currently not supported. 206 * 207 * @retval -1 The high-resolution timer fails to be started. 208 * @retval 0 The high-resolution timer is successfully started. 209 * @retval 1 The high-resolution timer node is already in the linked list. 210 Only the scheduled time will be updated and a new timer node will not be created. 211 * @par Dependency: 212 * <ul> 213 * <li>hrtimer.h: the header file that contains the API declaration.</li> 214 * </ul> 215 * @see hrtimer_cancel 216 */ 217 int hrtimer_start(struct hrtimer *timer, union ktime time, const enum hrtimer_mode mode); 218 219 /** 220 * @ingroup hr_swtmr 221 * @brief Delete an existing high-resolution timer. 222 * 223 * @par Description: 224 * This API is used to delete an existing high-resolution timer. The timeout callback function applied in 225 * an existing high-resolution timer is deleted first. If the timeout callback function linked list is not null 226 * after the function is deleted, the timer will not be deleted. If the timeout callback function linked list is null 227 * after the function is deleted, the timer will be deleted. 228 * @attention 229 * <ul> 230 * <li>If the pointer to the high-resolution timer is null or the timer node does not exist, 231 * the high-resolution timer fails to be deleted.</li> 232 * </ul> 233 * 234 * @param timer [IN] Pointer to the high-resolution timer structure to be deleted. 235 * 236 * @retval -1 The high-resolution timer fails to be deleted. 237 * @retval 0 The timer to be deleted does not exist. 238 * @retval 1 The timer is in scheduled state. 239 * @par Dependency: 240 * <ul> 241 * <li>hrtimer.h: the header file that contains the API declaration.</li> 242 * </ul> 243 * @see hrtimer_start 244 */ 245 int hrtimer_cancel(struct hrtimer *timer); 246 247 /** 248 * @ingroup hr_swtmr 249 * @brief Forward the expiry of an existing high-resolution timer. 250 * 251 * @par Description: 252 * This API is used to change the scheduled time of an existing high-resolution timer to 253 * the time specified by the passed-in parameter. 254 * @attention 255 * <ul> 256 * <li>If the timer does not exist, create a timer.</li> 257 * <li>The value of the scheduled time cannot be zero.</li> 258 * <li>This API does not fully adapt to Linux, please pay attention to the parameters.</li> 259 * </ul> 260 * 261 * @param timer [IN] Pointer to the high-resolution timer structure. 262 * @param interval [IN] Structure of the interval to forward. 263 * 264 * @retval 0 The timer expiry fails to be forwarded. 265 * @retval Timer value The timer expiry is successfully forwarded. 266 * @par Dependency: 267 * <ul> 268 * <li>hrtimer.h: the header file that contains the API declaration.</li> 269 * </ul> 270 * @see None. 271 */ 272 u64 hrtimer_forward(struct hrtimer *timer, union ktime interval); 273 274 /** 275 * @ingroup hr_swtmr 276 * @brief Check whether a specified high-resolution timer exists. 277 * 278 * @par Description: 279 * This API is used to determine whether a specified high-resolution timer exists by 280 * querying the global linked list of timer nodes. 281 * @attention 282 * <ul> 283 * <li>The return value is inconsistent with the Linux standard interface definition.</li> 284 * </ul> 285 * 286 * @param timer [IN] Pointer to the high-resolution timer structure to be checked. 287 * 288 * @retval #LOS_NOK 1: The queried timer does not exist. 289 * @retval #LOS_OK 0: The queried timer exists. 290 * @retval #-1 The value of the parameter timer is NULL. 291 * @par Dependency 292 * <ul> 293 * <li>hrtimer.h: the header file that contains the API declaration.</li> 294 * </ul> 295 * @see None. 296 */ 297 int hrtimer_is_queued(struct hrtimer *timer); 298 299 #ifdef __cplusplus 300 } 301 #endif /* __cplusplus */ 302 303 #endif /* _HRTIMER_H */ 304