remove trailing whitespaces
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_timer.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2012-2013 6WIND S.A.
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 #include <string.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <inttypes.h>
42 #include <sys/mman.h>
43 #include <sys/queue.h>
44 #include <pthread.h>
45 #include <errno.h>
46
47 #include <rte_common.h>
48 #include <rte_log.h>
49 #include <rte_cycles.h>
50 #include <rte_tailq.h>
51 #include <rte_memory.h>
52 #include <rte_memzone.h>
53 #include <rte_eal.h>
54 #include <rte_debug.h>
55
56 #include "eal_private.h"
57 #include "eal_internal_cfg.h"
58
59 enum timer_source eal_timer_source = EAL_TIMER_HPET;
60
61 /* The frequency of the RDTSC timer resolution */
62 static uint64_t eal_tsc_resolution_hz = 0;
63
64 #ifdef RTE_LIBEAL_USE_HPET
65
66 #define DEV_HPET "/dev/hpet"
67
68 /* Maximum number of counters. */
69 #define HPET_TIMER_NUM 3
70
71 /* General capabilities register */
72 #define CLK_PERIOD_SHIFT     32 /* Clock period shift. */
73 #define CLK_PERIOD_MASK      0xffffffff00000000ULL /* Clock period mask. */
74 #define COUNT_SIZE_CAP_SHIFT 13 /* Count size capa. shift. */
75 #define COUNT_SIZE_CAP_MASK 0x0000000000002000ULL /* Count size capa. mask. */
76
77 /**
78  * HPET timer registers. From the Intel IA-PC HPET (High Precision Event
79  * Timers) Specification.
80  */
81 struct eal_hpet_regs {
82         /* Memory-mapped, software visible registers */
83         uint64_t capabilities;      /**< RO General Capabilities Register. */
84         uint64_t reserved0;         /**< Reserved for future use. */
85         uint64_t config;            /**< RW General Configuration Register. */
86         uint64_t reserved1;         /**< Reserved for future use. */
87         uint64_t isr;               /**< RW Clear General Interrupt Status. */
88         uint64_t reserved2[25];     /**< Reserved for future use. */
89         union {
90                 uint64_t counter;   /**< RW Main Counter Value Register. */
91                 struct {
92                         uint32_t counter_l; /**< RW Main Counter Low. */
93                         uint32_t counter_h; /**< RW Main Counter High. */
94                 };
95         };
96         uint64_t reserved3;         /**< Reserved for future use. */
97         struct {
98                 uint64_t config;    /**< RW Timer Config and Capability Reg. */
99                 uint64_t comp;      /**< RW Timer Comparator Value Register. */
100                 uint64_t fsb;       /**< RW FSB Interrupt Route Register. */
101                 uint64_t reserved4; /**< Reserved for future use. */
102         } timers[HPET_TIMER_NUM]; /**< Set of HPET timers. */
103 };
104
105 /* Mmap'd hpet registers */
106 static volatile struct eal_hpet_regs *eal_hpet = NULL;
107
108 /* Period at which the HPET counter increments in
109  * femtoseconds (10^-15 seconds). */
110 static uint32_t eal_hpet_resolution_fs = 0;
111
112 /* Frequency of the HPET counter in Hz */
113 static uint64_t eal_hpet_resolution_hz = 0;
114
115 /* Incremented 4 times during one 32bits hpet full count */
116 static uint32_t eal_hpet_msb;
117
118 static pthread_t msb_inc_thread_id;
119
120 /*
121  * This function runs on a specific thread to update a global variable
122  * containing used to process MSB of the HPET (unfortunatelly, we need
123  * this because hpet is 32 bits by default under linux).
124  */
125 static void
126 hpet_msb_inc(__attribute__((unused)) void *arg)
127 {
128         uint32_t t;
129
130         while (1) {
131                 t = (eal_hpet->counter_l >> 30);
132                 if (t != (eal_hpet_msb & 3))
133                         eal_hpet_msb ++;
134                 sleep(10);
135         }
136 }
137
138 uint64_t
139 rte_get_hpet_hz(void)
140 {
141         if(internal_config.no_hpet)
142                 rte_panic("Error, HPET called, but no HPET present\n");
143
144         return eal_hpet_resolution_hz;
145 }
146
147 uint64_t
148 rte_get_hpet_cycles(void)
149 {
150         uint32_t t, msb;
151         uint64_t ret;
152
153         if(internal_config.no_hpet)
154                 rte_panic("Error, HPET called, but no HPET present\n");
155
156         t = eal_hpet->counter_l;
157         msb = eal_hpet_msb;
158         ret = (msb + 2 - (t >> 30)) / 4;
159         ret <<= 32;
160         ret += t;
161         return ret;
162 }
163
164 #endif
165
166
167 void
168 rte_delay_us(unsigned us)
169 {
170         const uint64_t start = rte_get_timer_cycles();
171         const uint64_t ticks = (uint64_t)us * rte_get_timer_hz() / 1E6;
172         while ((rte_get_timer_cycles() - start) < ticks)
173                 rte_pause();
174 }
175
176 uint64_t
177 rte_get_tsc_hz(void)
178 {
179         return eal_tsc_resolution_hz;
180 }
181
182
183 #ifdef RTE_LIBEAL_USE_HPET
184 /*
185  * Open and mmap /dev/hpet (high precision event timer) that will
186  * provide our time reference.
187  */
188 int
189 rte_eal_hpet_init(int make_default)
190 {
191         int fd, ret;
192
193         if (internal_config.no_hpet) {
194                 RTE_LOG(INFO, EAL, "HPET is disabled\n");
195                 return -1;
196         }
197
198         fd = open(DEV_HPET, O_RDONLY);
199         if (fd < 0) {
200                 RTE_LOG(ERR, EAL, "ERROR: Cannot open "DEV_HPET": %s!\n",
201                         strerror(errno));
202                 internal_config.no_hpet = 1;
203                 return -1;
204         }
205         eal_hpet = mmap(NULL, 1024, PROT_READ, MAP_SHARED, fd, 0);
206         if (eal_hpet == MAP_FAILED) {
207                 RTE_LOG(ERR, EAL, "ERROR: Cannot mmap "DEV_HPET"!\n"
208                                 "Please enable CONFIG_HPET_MMAP in your kernel configuration "
209                                 "to allow HPET support.\n"
210                                 "To run without using HPET, set CONFIG_RTE_LIBEAL_USE_HPET=n "
211                                 "in your build configuration or use '--no-hpet' EAL flag.\n");
212                 close(fd);
213                 internal_config.no_hpet = 1;
214                 return -1;
215         }
216         close(fd);
217
218         eal_hpet_resolution_fs = (uint32_t)((eal_hpet->capabilities &
219                                         CLK_PERIOD_MASK) >>
220                                         CLK_PERIOD_SHIFT);
221
222         eal_hpet_resolution_hz = (1000ULL*1000ULL*1000ULL*1000ULL*1000ULL) /
223                 (uint64_t)eal_hpet_resolution_fs;
224
225         RTE_LOG(INFO, EAL, "HPET frequency is ~%"PRIu64" kHz\n",
226                         eal_hpet_resolution_hz/1000);
227
228         eal_hpet_msb = (eal_hpet->counter_l >> 30);
229
230         /* create a thread that will increment a global variable for
231          * msb (hpet is 32 bits by default under linux) */
232         ret = pthread_create(&msb_inc_thread_id, NULL,
233                         (void *(*)(void *))hpet_msb_inc, NULL);
234         if (ret < 0) {
235                 RTE_LOG(ERR, EAL, "ERROR: Cannot create HPET timer thread!\n");
236                 internal_config.no_hpet = 1;
237                 return -1;
238         }
239
240         if (make_default)
241                 eal_timer_source = EAL_TIMER_HPET;
242         return 0;
243 }
244 #endif
245
246 static void
247 check_tsc_flags(void)
248 {
249         char line[512];
250         FILE *stream;
251
252         stream = fopen("/proc/cpuinfo", "r");
253         if (!stream) {
254                 RTE_LOG(WARNING, EAL, "WARNING: Unable to open /proc/cpuinfo\n");
255                 return;
256         }
257
258         while (fgets(line, sizeof line, stream)) {
259                 char *constant_tsc;
260                 char *nonstop_tsc;
261
262                 if (strncmp(line, "flags", 5) != 0)
263                         continue;
264
265                 constant_tsc = strstr(line, "constant_tsc");
266                 nonstop_tsc = strstr(line, "nonstop_tsc");
267                 if (!constant_tsc || !nonstop_tsc)
268                         RTE_LOG(WARNING, EAL,
269                                 "WARNING: cpu flags "
270                                 "constant_tsc=%s "
271                                 "nonstop_tsc=%s "
272                                 "-> using unreliable clock cycles !\n",
273                                 constant_tsc ? "yes":"no",
274                                 nonstop_tsc ? "yes":"no");
275                 break;
276         }
277
278         fclose(stream);
279 }
280
281 static int
282 set_tsc_freq_from_clock(void)
283 {
284 #ifdef CLOCK_MONOTONIC_RAW
285 #define NS_PER_SEC 1E9
286
287         struct timespec sleeptime = {.tv_nsec = 5E8 }; /* 1/2 second */
288
289         struct timespec t_start, t_end;
290
291         if (clock_gettime(CLOCK_MONOTONIC_RAW, &t_start) == 0) {
292                 uint64_t ns, end, start = rte_rdtsc();
293                 nanosleep(&sleeptime,NULL);
294                 clock_gettime(CLOCK_MONOTONIC_RAW, &t_end);
295                 end = rte_rdtsc();
296                 ns = ((t_end.tv_sec - t_start.tv_sec) * NS_PER_SEC);
297                 ns += (t_end.tv_nsec - t_start.tv_nsec);
298
299                 double secs = (double)ns/NS_PER_SEC;
300                 eal_tsc_resolution_hz = (uint64_t)((end - start)/secs);
301                 return 0;
302         }
303 #endif
304         return -1;
305 }
306
307 static void
308 set_tsc_freq_fallback(void)
309 {
310         RTE_LOG(WARNING, EAL, "WARNING: clock_gettime cannot use "
311                         "CLOCK_MONOTONIC_RAW and HPET is not available"
312                         " - clock timings may be less accurate.\n");
313         /* assume that the sleep(1) will sleep for 1 second */
314         uint64_t start = rte_rdtsc();
315         sleep(1);
316         eal_tsc_resolution_hz = rte_rdtsc() - start;
317 }
318 /*
319  * This function measures the TSC frequency. It uses a variety of approaches.
320  *
321  * 1. If kernel provides CLOCK_MONOTONIC_RAW we use that to tune the TSC value
322  * 2. If kernel does not provide that, and we have HPET support, tune using HPET
323  * 3. Lastly, if neither of the above can be used, just sleep for 1 second and
324  * tune off that, printing a warning about inaccuracy of timing
325  */
326 static void
327 set_tsc_freq(void)
328 {
329         if (set_tsc_freq_from_clock() < 0)
330                 set_tsc_freq_fallback();
331
332         RTE_LOG(INFO, EAL, "TSC frequency is ~%"PRIu64" KHz\n",
333                         eal_tsc_resolution_hz/1000);
334 }
335
336 int
337 rte_eal_timer_init(void)
338 {
339
340         eal_timer_source = EAL_TIMER_TSC;
341
342         set_tsc_freq();
343         check_tsc_flags();
344         return 0;
345 }