1 /* 2 * Copyright (C) 2018-2022 Intel Corporation. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #ifndef COMMON_TIMER_H 8 #define COMMON_TIMER_H 9 10 #include <list.h> 11 #include <ticks.h> 12 13 /** 14 * @brief Timer 15 * 16 * @defgroup timer ACRN Timer 17 * @{ 18 */ 19 20 typedef void (*timer_handle_t)(void *data); 21 22 /** 23 * @brief Definition of timer tick mode 24 */ 25 enum tick_mode { 26 TICK_MODE_ONESHOT = 0, /**< one-shot mode */ 27 TICK_MODE_PERIODIC, /**< periodic mode */ 28 }; 29 30 /** 31 * @brief Definition of timers for per-cpu 32 */ 33 struct per_cpu_timers { 34 struct list_head timer_list; /**< it's for runtime active timer list */ 35 }; 36 37 /** 38 * @brief Definition of timer 39 */ 40 struct hv_timer { 41 struct list_head node; /**< link all timers */ 42 enum tick_mode mode; /**< timer mode: one-shot or periodic */ 43 uint64_t timeout; /**< tsc deadline to interrupt */ 44 uint64_t period_in_cycle; /**< period of the periodic timer in CPU ticks */ 45 timer_handle_t func; /**< callback if time reached */ 46 void *priv_data; /**< func private data */ 47 }; 48 49 /* External Interfaces */ 50 51 /** 52 * @brief Initialize a timer structure. 53 * 54 * @param[in] timer Pointer to timer. 55 * @param[in] func irq callback if time reached. 56 * @param[in] priv_data func private data. 57 * @param[in] timeout tsc deadline to interrupt. 58 * @param[in] period_in_cycle period of the periodic timer in unit of TSC cycles. 59 * 60 * @remark Don't initialize a timer twice if it has been added to the timer list 61 * after calling add_timer. If you want to, delete the timer from the list first. 62 */ 63 void initialize_timer(struct hv_timer *timer, 64 timer_handle_t func, void *priv_data, 65 uint64_t timeout, uint64_t period_in_cycle); 66 67 /** 68 * @brief Check a timer whether expired. 69 * 70 * @param[in] timer Pointer to timer. 71 * @param[in] now to compare. 72 * @param[in] delta Pointer to return the delta (timeout - now) if timer is not expired. 73 * 74 * @retval true if the timer is expired, false otherwise. 75 */ 76 bool timer_expired(const struct hv_timer *timer, uint64_t now, uint64_t *delta); 77 78 /** 79 * @brief Check if a timer is active (in the timer list) or not. 80 * 81 * @param[in] timer Pointer to timer. 82 * 83 * @retval true if the timer is in timer list, false otherwise. 84 */ 85 bool timer_is_started(const struct hv_timer *timer); 86 87 /** 88 * @brief Update a timer. 89 * 90 * @param[in] timer Pointer to timer. 91 * @param[in] timeout deadline to interrupt. 92 * @param[in] period period of the periodic timer in unit of CPU ticks. 93 */ 94 void update_timer(struct hv_timer *timer, uint64_t timeout, uint64_t period); 95 96 /** 97 * @brief Add a timer. 98 * 99 * @param[in] timer Pointer to timer. 100 * 101 * @retval 0 on success 102 * @retval -EINVAL timer has an invalid value 103 * 104 * @remark Don't call it in the timer callback function or interrupt content. 105 */ 106 int32_t add_timer(struct hv_timer *timer); 107 108 /** 109 * @brief Delete a timer. 110 * 111 * @param[in] timer Pointer to timer. 112 * 113 * @remark Don't call it in the timer callback function or interrupt content. 114 */ 115 void del_timer(struct hv_timer *timer); 116 117 /** 118 * @brief Initialize timer. 119 */ 120 void timer_init(void); 121 122 /** 123 * @} 124 */ 125 126 #endif /* COMMON_TIMER_H */ 127