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