a490905728d0ea61b7cdc4f3413e857721338e42
[dpdk.git] / lib / librte_eal / common / include / rte_cycles.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34 /*   BSD LICENSE
35  *
36  *   Copyright(c) 2013 6WIND.
37  *
38  *   Redistribution and use in source and binary forms, with or without
39  *   modification, are permitted provided that the following conditions
40  *   are met:
41  *
42  *     * Redistributions of source code must retain the above copyright
43  *       notice, this list of conditions and the following disclaimer.
44  *     * Redistributions in binary form must reproduce the above copyright
45  *       notice, this list of conditions and the following disclaimer in
46  *       the documentation and/or other materials provided with the
47  *       distribution.
48  *     * Neither the name of 6WIND S.A. nor the names of its
49  *       contributors may be used to endorse or promote products derived
50  *       from this software without specific prior written permission.
51  *
52  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63  */
64
65 #ifndef _RTE_CYCLES_H_
66 #define _RTE_CYCLES_H_
67
68 /**
69  * @file
70  *
71  * Simple Time Reference Functions (Cycles and HPET).
72  */
73
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77
78 #include <stdint.h>
79 #include <rte_debug.h>
80
81 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
82 /** Global switch to use VMWARE mapping of TSC instead of RDTSC */
83 extern int rte_cycles_vmware_tsc_map;
84 #include <rte_branch_prediction.h>
85 #endif
86
87 #define MS_PER_S 1000
88 #define US_PER_S 1000000
89 #define NS_PER_S 1000000000
90
91 enum timer_source {
92         EAL_TIMER_TSC = 0,
93         EAL_TIMER_HPET
94 };
95 extern enum timer_source eal_timer_source;
96
97 /**
98  * Read the TSC register.
99  *
100  * @return
101  *   The TSC for this lcore.
102  */
103 static inline uint64_t
104 rte_rdtsc(void)
105 {
106         union {
107                 uint64_t tsc_64;
108                 struct {
109                         uint32_t lo_32;
110                         uint32_t hi_32;
111                 };
112         } tsc;
113
114 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
115         if (unlikely(rte_cycles_vmware_tsc_map)) {
116                 /* ecx = 0x10000 corresponds to the physical TSC for VMware */
117                 asm volatile("rdpmc" :
118                              "=a" (tsc.lo_32),
119                              "=d" (tsc.hi_32) :
120                              "c"(0x10000));
121                 return tsc.tsc_64;
122         }
123 #endif
124
125         asm volatile("rdtsc" :
126                      "=a" (tsc.lo_32),
127                      "=d" (tsc.hi_32));
128         return tsc.tsc_64;
129 }
130
131 /**
132  * Get the measured frequency of the RDTSC counter
133  *
134  * @return
135  *   The TSC frequency for this lcore
136  */
137 uint64_t
138 rte_get_tsc_hz(void);
139
140 /**
141  * Return the number of TSC cycles since boot
142  *
143   * @return
144  *   the number of cycles
145  */
146 static inline uint64_t
147 rte_get_tsc_cycles(void) { return rte_rdtsc(); }
148
149 #ifdef RTE_LIBEAL_USE_HPET
150 /**
151  * Return the number of HPET cycles since boot
152  *
153  * This counter is global for all execution units. The number of
154  * cycles in one second can be retrieved using rte_get_hpet_hz().
155  *
156  * @return
157  *   the number of cycles
158  */
159 uint64_t
160 rte_get_hpet_cycles(void);
161
162 /**
163  * Get the number of HPET cycles in one second.
164  *
165  * @return
166  *   The number of cycles in one second.
167  */
168 uint64_t
169 rte_get_hpet_hz(void);
170
171 /**
172  * Initialise the HPET for use. This must be called before the rte_get_hpet_hz
173  * and rte_get_hpet_cycles APIs are called. If this function does not succeed,
174  * then the HPET functions are unavailable and should not be called.
175  *
176  * @param make_default
177  *      If set, the hpet timer becomes the default timer whose values are
178  *      returned by the rte_get_timer_hz/cycles API calls
179  *
180  * @return
181  *      0 on success,
182  *      -1 on error, and the make_default parameter is ignored.
183  */
184 int rte_eal_hpet_init(int make_default);
185
186 #endif
187
188 /**
189  * Get the number of cycles since boot from the default timer.
190  *
191  * @return
192  *   The number of cycles
193  */
194 static inline uint64_t
195 rte_get_timer_cycles(void)
196 {
197         switch(eal_timer_source) {
198         case EAL_TIMER_TSC:
199                 return rte_rdtsc();
200         case EAL_TIMER_HPET:
201 #ifdef RTE_LIBEAL_USE_HPET
202                 return rte_get_hpet_cycles();
203 #endif
204         default: rte_panic("Invalid timer source specified\n");
205         }
206 }
207
208 /**
209  * Get the number of cycles in one second for the default timer.
210  *
211  * @return
212  *   The number of cycles in one second.
213  */
214 static inline uint64_t
215 rte_get_timer_hz(void)
216 {
217         switch(eal_timer_source) {
218         case EAL_TIMER_TSC:
219                 return rte_get_tsc_hz();
220         case EAL_TIMER_HPET:
221 #ifdef RTE_LIBEAL_USE_HPET
222                 return rte_get_hpet_hz();
223 #endif
224         default: rte_panic("Invalid timer source specified\n");
225         }
226 }
227
228 /**
229  * Wait at least us microseconds.
230  *
231  * @param us
232  *   The number of microseconds to wait.
233  */
234 void
235 rte_delay_us(unsigned us);
236
237 /**
238  * Wait at least ms milliseconds.
239  *
240  * @param ms
241  *   The number of milliseconds to wait.
242  */
243 static inline void
244 rte_delay_ms(unsigned ms)
245 {
246         rte_delay_us(ms * 1000);
247 }
248
249 #ifdef __cplusplus
250 }
251 #endif
252
253 #endif /* _RTE_CYCLES_H_ */