• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-
2  * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice unmodified, this list of conditions, and the following
9  *    disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD: releng/12.2/sys/compat/linuxkpi/common/include/linux/hrtimer.h 334482 2018-06-01 11:33:14Z hselasky $
26  */
27 
28 #ifndef _LINUX_HRTIMER_H_
29 #define	_LINUX_HRTIMER_H_
30 
31 #include "sys/types.h"
32 #include "los_base.h"
33 #include "los_task.h"
34 
35 #ifdef __cplusplus
36 #if __cplusplus
37 extern "C" {
38 #endif /* __cplusplus */
39 #endif /* __cplusplus */
40 
41 
42 /**
43  * @ingroup hr_swtmr
44  * Define the number of timer cycles in 1us.
45  */
46 #define HRTIMER_PERUS  (OS_SYS_CLOCK / 1000000.0)
47 
48 /**
49  * @ingroup hr_swtmr
50  * Define s64 as a signed long integer.
51  */
52 typedef signed long long s64;
53 
54 /**
55  * @ingroup hr_swtmr
56  * Define s32 as a signed long integer.
57  */
58 typedef signed int s32;
59 
60 /**
61  * @ingroup hr_swtmr
62  * Define u64 as an unsigned long integer.
63  */
64 typedef unsigned long long u64;
65 
66 /**
67  * @ingroup hr_swtmr
68  * Structure of the scheduled time.
69  */
70 union ktime {
71     s64 tv64;            /**< Scheduled time for 64-bit CPU systems. */
72     struct {
73         s32 sec, usec;   /**< Scheduled time for 32-bit CPU systems. */
74     } tv;
75 };
76 
77 /**
78  * @ingroup hr_swtmr
79  * Structure of a node in a high-resolution timer queue.
80  */
81 struct timerqueue_node {
82     unsigned int node;   /**< Not in use temporarily. */
83 };
84 
85 /**
86  * @ingroup hr_swtmr
87  * Enumerative structure of the high-resolution timer mode arguments.
88  */
89 enum hrtimer_mode {
90     HRTIMER_MODE_ABS = 0x0,     /**< Time value is absolute. */
91     HRTIMER_MODE_REL = 0x1,     /**< Time value is relative to now. */
92     HRTIMER_MODE_PINNED = 0x2,  /**< Timer is bound to CPU. */
93 };
94 
95 /**
96  * @ingroup hr_swtmr
97  * Enumerative structure of the return type of a high-resolution timer timeout callback function.
98  */
99 enum hrtimer_restart {
100     HRTIMER_NORESTART,   /**< The timer will not be restarted.*/
101     HRTIMER_RESTART      /**< The timer must be restarted.*/
102 };
103 struct hrtimer;
104 
105 /**
106  * @ingroup hr_swtmr
107  * Define the function handler type of a high-resolution timer timeout callback function.
108  */
109 typedef enum hrtimer_restart (*Handler)(struct hrtimer *);
110 
111 /**
112  * @ingroup hr_swtmr
113  * Structure of parameters of a high-resolution timer API.
114  */
115 struct hrtimer {
116     union ktime _softexpires;             /**< Structure of the scheduled time. */
117     Handler function;                     /**< Timeout callback function. */
118     unsigned long state;                  /**< Timer working state. Not in use temporarily. */
119 #ifdef CONFIG_TIMER_STATS
120     int start_pid;                        /**< ID of the task that invokes a high-resolution timer.
121                                                Not in use temporarily. */
122     void *start_site;                     /**< Function that invokes a high-resolution timer. Not in use temporarily. */
123 #define START_TASK_NAME_LEN  16
124     char start_comm[START_TASK_NAME_LEN]; /**< Name of the task that invokes a high-resolution timer.
125                                                Not in use temporarily. */
126 #endif
127 };
128 
129 /**
130  * @ingroup hr_swtmr
131  * Parameter structure of the nodes of a high-resolution timer timeout callback function.
132  */
133 struct handler_list_node {
134     struct handler_list_node *pstNext;  /**< Pointer to the next node. */
135     Handler pfnHandler;                 /**< Timeout callback function. */
136     union ktime _softexpires;           /**< Structure of the scheduled time. */
137 };
138 
139 /**
140  * @ingroup hr_swtmr
141  * Parameter structure of a high-resolution timer node.
142  */
143 struct hrtimer_list_node {
144     struct hrtimer_list_node *pstNext;     /**< Pointer to the next node. */
145     struct handler_list_node *HandlerHead; /**< Pointer to the node queue of a timeout callback function. */
146     unsigned int set_time_reload;          /**< Count of timers. */
147     union ktime _softexpires;              /**< Structure of the scheduled time. */
148 };
149 
150 #define hrtimer_init(timer, clockID, mode) \
151     linux_hrtimer_init(timer, clockID, mode)
152 
153 #define hrtimer_create(timer, time, handler) \
154     linux_hrtimer_create(timer, time, handler)
155 
156 #define hrtimer_start(timer, time, mode) \
157     linux_hrtimer_start(timer, time, mode)
158 
159 #define hrtimer_cancel(timer) \
160     linux_hrtimer_cancel(timer)
161 
162 #define hrtimer_forward(timer, interval) \
163     linux_hrtimer_forward(timer, interval)
164 
165 #define hrtimer_is_queued(timer) \
166     linux_hrtimer_is_queued(timer)
167 
168 /**
169  * @ingroup hr_swtmr
170  * @brief Initialize a high-resolution timer.
171  *
172  * @par Description:
173  * This API is used to initialize a high-resolution timer to the given clock.
174  * @attention
175  * <ul>
176  * <li>The pointer to the high-resolution timer structure to be initialized must not be null.</li>
177  * </ul>
178  *
179  * @param timer      [IN] Pointer to the high-resolution timer structure.
180  * @param clockID    [IN] This parameter is not supported, so users can pass in any integer.
181  * @param mode       [IN] Mode setting is currently not supported by Huawei LiteOS.
182  *
183  * @retval   None.
184  * @par Dependency:
185  * <ul>
186  * <li>hrtimer.h: the header file that contains the API declaration.</li>
187  * </ul>
188  * @see None.
189  */
190 void linux_hrtimer_init(struct hrtimer *timer, clockid_t clockID, enum hrtimer_mode mode);
191 
192 /**
193  * @ingroup hr_swtmr
194  * @brief Create a high-resolution timer.
195  *
196  * @par Description:
197  * This API is used to create a high-resolution timer node and initialize timer parameters.
198  * @attention
199  * <ul>
200  * <li>The passed-in pointer to the high-resolution timer structure must not be null.</li>
201  * <li>The value of the scheduled time cannot be zero.</li>
202  * </ul>
203  *
204  * @param timer      [IN] Pointer to the high-resolution timer structure.
205  * @param time       [IN] Structure of the scheduled time.
206  * @param handler    [IN] Pointer to the timeout callback function.
207  *
208  * @retval  -1        The high-resolution timer fails to be created because the pointer to
209  * the high-resolution timer structure is null.
210  * @retval    0         The high-resolution timer is successfully created.
211  * @par Dependency:
212  * <ul>
213  * <li>hrtimer.h: the header file that contains the API declaration.</li>
214  * </ul>
215  * @see None.
216  */
217 int linux_hrtimer_create(struct hrtimer *timer, union ktime time, Handler handler);
218 
219 /**
220  * @ingroup hr_swtmr
221  * @brief Start a high-resolution timer.
222  *
223  * @par Description:
224  * This API is used to add a high-resolution timer node to the global linked list and start timing.
225  * @attention
226  * <ul>
227  * <li>The passed-in pointer to the high-resolution timer structure must not be null.</li>
228  * <li>The value of the scheduled time cannot be zero.</li>
229  * </ul>
230  *
231  * @param timer        [IN] Pointer to the high-resolution timer structure.
232  * @param time         [IN] Structure of the scheduled time.
233  * @param mode         [IN] Mode setting is currently not supported by Huawei LiteOS.
234  *
235  * @retval    -1       The high-resolution timer fails to be started.
236  * @retval    0        The high-resolution timer is successfully started.
237  * @retval    1        The high-resolution timer node is already in the linked list.
238                        Only the scheduled time will be updated and a new timer node will not be created.
239  * @par Dependency:
240  * <ul>
241  * <li>hrtimer.h: the header file that contains the API declaration.</li>
242  * </ul>
243  * @see hrtimer_cancel
244  */
245 int linux_hrtimer_start(struct hrtimer *timer, union ktime time, const enum hrtimer_mode mode);
246 
247 /**
248  * @ingroup hr_swtmr
249  * @brief Delete an existing high-resolution timer.
250  *
251  * @par Description:
252  * This API is used to delete an existing high-resolution timer. The timeout callback function applied in
253  * an existing high-resolution timer is deleted first. If the timeout callback function linked list is not null
254  * after the function is deleted, the timer will not be deleted. If the timeout callback function linked list is null
255  * after the function is deleted, the timer will be deleted.
256  * @attention
257  * <ul>
258  * <li>If the pointer to the high-resolution timer is null or the timer node does not exist,
259  * the high-resolution timer fails to be deleted.</li>
260  * </ul>
261  *
262  * @param timer        [IN] Pointer to the high-resolution timer structure to be deleted.
263  *
264  * @retval    -1       The high-resolution timer fails to be deleted.
265  * @retval    0        The timer to be deleted does not exist.
266  * @retval    1        The timer is in scheduled state.
267  * @par Dependency:
268  * <ul>
269  * <li>hrtimer.h: the header file that contains the API declaration.</li>
270  * </ul>
271  * @see hrtimer_start
272  */
273 int linux_hrtimer_cancel(struct hrtimer *timer);
274 
275 /**
276  * @ingroup hr_swtmr
277  * @brief Forward the expiry of an existing high-resolution timer.
278  *
279  * @par Description:
280  * This API is used to change the scheduled time of an existing high-resolution timer to
281  * the time specified by the passed-in parameter.
282  * @attention
283  * <ul>
284  * <li>If the timer does not exist, create a timer.</li>
285  * <li>The value of the scheduled time cannot be zero.</li>
286  * </ul>
287  *
288  * @param timer        [IN] Pointer to the high-resolution timer structure.
289  * @param interval     [IN] Structure of the interval to forward.
290  *
291  * @retval    0       The timer expiry fails to be forwarded.
292  * @retval    Timer value    The timer expiry is successfully forwarded.
293  * @par Dependency:
294  * <ul>
295  * <li>hitimer.h: the header file that contains the API declaration.</li>
296  * </ul>
297  * @see None.
298  */
299 u64 linux_hrtimer_forward(struct hrtimer *timer, union ktime interval);
300 
301 /**
302  * @ingroup hr_swtmr
303  * @brief Check whether a specified high-resolution timer exists.
304  *
305  * @par Description:
306  * This API is used to determine whether a specified high-resolution timer exists by
307  * querying the global linked list of timer nodes.
308  * @attention
309  * <ul>
310  * <li>None.</li>
311  * </ul>
312  *
313  * @param timer        [IN] Pointer to the high-resolution timer structure to be checked.
314  *
315  * @retval    #LOS_NOK   1: The queried timer does not exist.
316  * @retval    #LOS_OK    0: The queried timer exists.
317  * @retval    #-1           The value of the parameter timer is NULL.
318  * @par Dependency
319  * <ul>
320  * <li>hrtimer.h: the header file that contains the API declaration.</li>
321  * </ul>
322  * @see None.
323  */
324 int linux_hrtimer_is_queued(struct hrtimer *timer);
325 
326 #ifdef __cplusplus
327 #if __cplusplus
328 }
329 #endif /* __cplusplus */
330 #endif /* __cplusplus */
331 
332 #endif /* _HRTIMER_H */
333