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