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