1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation. 3 * Copyright(c) 2013 6WIND S.A. 4 */ 5 6 #ifndef _RTE_CYCLES_H_ 7 #define _RTE_CYCLES_H_ 8 9 /** 10 * @file 11 * 12 * Simple Time Reference Functions (Cycles and HPET). 13 */ 14 15 #include <stdint.h> 16 #include <rte_compat.h> 17 #include <rte_debug.h> 18 #include <rte_atomic.h> 19 20 #define MS_PER_S 1000 21 #define US_PER_S 1000000 22 #define NS_PER_S 1000000000 23 24 enum timer_source { 25 EAL_TIMER_TSC = 0, 26 EAL_TIMER_HPET 27 }; 28 extern enum timer_source eal_timer_source; 29 30 /** 31 * Get the measured frequency of the RDTSC counter 32 * 33 * @return 34 * The TSC frequency for this lcore 35 */ 36 uint64_t 37 rte_get_tsc_hz(void); 38 39 /** 40 * Return the number of TSC cycles since boot 41 * 42 * @return 43 * the number of cycles 44 */ 45 static inline uint64_t 46 rte_get_tsc_cycles(void); 47 48 #ifdef RTE_LIBEAL_USE_HPET 49 /** 50 * Return the number of HPET cycles since boot 51 * 52 * This counter is global for all execution units. The number of 53 * cycles in one second can be retrieved using rte_get_hpet_hz(). 54 * 55 * @return 56 * the number of cycles 57 */ 58 uint64_t 59 rte_get_hpet_cycles(void); 60 61 /** 62 * Get the number of HPET cycles in one second. 63 * 64 * @return 65 * The number of cycles in one second. 66 */ 67 uint64_t 68 rte_get_hpet_hz(void); 69 70 /** 71 * Initialise the HPET for use. This must be called before the rte_get_hpet_hz 72 * and rte_get_hpet_cycles APIs are called. If this function does not succeed, 73 * then the HPET functions are unavailable and should not be called. 74 * 75 * @param make_default 76 * If set, the hpet timer becomes the default timer whose values are 77 * returned by the rte_get_timer_hz/cycles API calls 78 * 79 * @return 80 * 0 on success, 81 * -1 on error, and the make_default parameter is ignored. 82 */ 83 int rte_eal_hpet_init(int make_default); 84 85 #endif 86 87 /** 88 * Get the number of cycles since boot from the default timer. 89 * 90 * @return 91 * The number of cycles 92 */ 93 static inline uint64_t 94 rte_get_timer_cycles(void) 95 { 96 #ifdef RTE_LIBEAL_USE_HPET 97 switch(eal_timer_source) { 98 case EAL_TIMER_TSC: 99 #endif 100 return rte_get_tsc_cycles(); 101 #ifdef RTE_LIBEAL_USE_HPET 102 case EAL_TIMER_HPET: 103 return rte_get_hpet_cycles(); 104 default: rte_panic("Invalid timer source specified\n"); 105 } 106 #endif 107 } 108 109 /** 110 * Get the number of cycles in one second for the default timer. 111 * 112 * @return 113 * The number of cycles in one second. 114 */ 115 static inline uint64_t 116 rte_get_timer_hz(void) 117 { 118 #ifdef RTE_LIBEAL_USE_HPET 119 switch(eal_timer_source) { 120 case EAL_TIMER_TSC: 121 #endif 122 return rte_get_tsc_hz(); 123 #ifdef RTE_LIBEAL_USE_HPET 124 case EAL_TIMER_HPET: 125 return rte_get_hpet_hz(); 126 default: rte_panic("Invalid timer source specified\n"); 127 } 128 #endif 129 } 130 /** 131 * Wait at least us microseconds. 132 * This function can be replaced with user-defined function. 133 * @see rte_delay_us_callback_register 134 * 135 * @param us 136 * The number of microseconds to wait. 137 */ 138 extern void 139 (*rte_delay_us)(unsigned int us); 140 141 /** 142 * Wait at least ms milliseconds. 143 * 144 * @param ms 145 * The number of milliseconds to wait. 146 */ 147 static inline void 148 rte_delay_ms(unsigned ms) 149 { 150 rte_delay_us(ms * 1000); 151 } 152 153 /** 154 * Blocking delay function. 155 * 156 * @param us 157 * Number of microseconds to wait. 158 */ 159 void rte_delay_us_block(unsigned int us); 160 161 /** 162 * Delay function that uses system sleep. 163 * Does not block the CPU core. 164 * 165 * @param us 166 * Number of microseconds to wait. 167 */ 168 __rte_experimental 169 void 170 rte_delay_us_sleep(unsigned int us); 171 172 /** 173 * Replace rte_delay_us with user defined function. 174 * 175 * @param userfunc 176 * User function which replaces rte_delay_us. rte_delay_us_block restores 177 * builtin block delay function. 178 */ 179 void rte_delay_us_callback_register(void(*userfunc)(unsigned int)); 180 181 #endif /* _RTE_CYCLES_H_ */ 182