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