4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright(c) 2012-2013 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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.
43 #include <sys/queue.h>
47 #include <rte_common.h>
49 #include <rte_cycles.h>
50 #include <rte_tailq.h>
51 #include <rte_memory.h>
52 #include <rte_memzone.h>
54 #include <rte_debug.h>
56 #include "eal_private.h"
57 #include "eal_internal_cfg.h"
59 enum timer_source eal_timer_source = EAL_TIMER_HPET;
61 /* The frequency of the RDTSC timer resolution */
62 static uint64_t eal_tsc_resolution_hz = 0;
64 #ifdef RTE_LIBEAL_USE_HPET
66 #define DEV_HPET "/dev/hpet"
68 /* Maximum number of counters. */
69 #define HPET_TIMER_NUM 3
71 /* General capabilities register */
72 #define CLK_PERIOD_SHIFT 32 /* Clock period shift. */
73 #define CLK_PERIOD_MASK 0xffffffff00000000ULL /* Clock period mask. */
76 * HPET timer registers. From the Intel IA-PC HPET (High Precision Event
77 * Timers) Specification.
79 struct eal_hpet_regs {
80 /* Memory-mapped, software visible registers */
81 uint64_t capabilities; /**< RO General Capabilities Register. */
82 uint64_t reserved0; /**< Reserved for future use. */
83 uint64_t config; /**< RW General Configuration Register. */
84 uint64_t reserved1; /**< Reserved for future use. */
85 uint64_t isr; /**< RW Clear General Interrupt Status. */
86 uint64_t reserved2[25]; /**< Reserved for future use. */
88 uint64_t counter; /**< RW Main Counter Value Register. */
90 uint32_t counter_l; /**< RW Main Counter Low. */
91 uint32_t counter_h; /**< RW Main Counter High. */
94 uint64_t reserved3; /**< Reserved for future use. */
96 uint64_t config; /**< RW Timer Config and Capability Reg. */
97 uint64_t comp; /**< RW Timer Comparator Value Register. */
98 uint64_t fsb; /**< RW FSB Interrupt Route Register. */
99 uint64_t reserved4; /**< Reserved for future use. */
100 } timers[HPET_TIMER_NUM]; /**< Set of HPET timers. */
103 /* Mmap'd hpet registers */
104 static volatile struct eal_hpet_regs *eal_hpet = NULL;
106 /* Period at which the HPET counter increments in
107 * femtoseconds (10^-15 seconds). */
108 static uint32_t eal_hpet_resolution_fs = 0;
110 /* Frequency of the HPET counter in Hz */
111 static uint64_t eal_hpet_resolution_hz = 0;
113 /* Incremented 4 times during one 32bits hpet full count */
114 static uint32_t eal_hpet_msb;
116 static pthread_t msb_inc_thread_id;
119 * This function runs on a specific thread to update a global variable
120 * containing used to process MSB of the HPET (unfortunatelly, we need
121 * this because hpet is 32 bits by default under linux).
124 hpet_msb_inc(__attribute__((unused)) void *arg)
129 t = (eal_hpet->counter_l >> 30);
130 if (t != (eal_hpet_msb & 3))
137 rte_get_hpet_hz(void)
139 if(internal_config.no_hpet)
140 rte_panic("Error, HPET called, but no HPET present\n");
142 return eal_hpet_resolution_hz;
146 rte_get_hpet_cycles(void)
151 if(internal_config.no_hpet)
152 rte_panic("Error, HPET called, but no HPET present\n");
154 t = eal_hpet->counter_l;
156 ret = (msb + 2 - (t >> 30)) / 4;
166 rte_delay_us(unsigned us)
168 const uint64_t start = rte_get_timer_cycles();
169 const uint64_t ticks = (uint64_t)us * rte_get_timer_hz() / 1E6;
170 while ((rte_get_timer_cycles() - start) < ticks)
177 return eal_tsc_resolution_hz;
181 #ifdef RTE_LIBEAL_USE_HPET
183 * Open and mmap /dev/hpet (high precision event timer) that will
184 * provide our time reference.
187 rte_eal_hpet_init(int make_default)
191 if (internal_config.no_hpet) {
192 RTE_LOG(INFO, EAL, "HPET is disabled\n");
196 fd = open(DEV_HPET, O_RDONLY);
198 RTE_LOG(ERR, EAL, "ERROR: Cannot open "DEV_HPET": %s!\n",
200 internal_config.no_hpet = 1;
203 eal_hpet = mmap(NULL, 1024, PROT_READ, MAP_SHARED, fd, 0);
204 if (eal_hpet == MAP_FAILED) {
205 RTE_LOG(ERR, EAL, "ERROR: Cannot mmap "DEV_HPET"!\n"
206 "Please enable CONFIG_HPET_MMAP in your kernel configuration "
207 "to allow HPET support.\n"
208 "To run without using HPET, set CONFIG_RTE_LIBEAL_USE_HPET=n "
209 "in your build configuration or use '--no-hpet' EAL flag.\n");
211 internal_config.no_hpet = 1;
216 eal_hpet_resolution_fs = (uint32_t)((eal_hpet->capabilities &
220 eal_hpet_resolution_hz = (1000ULL*1000ULL*1000ULL*1000ULL*1000ULL) /
221 (uint64_t)eal_hpet_resolution_fs;
223 RTE_LOG(INFO, EAL, "HPET frequency is ~%"PRIu64" kHz\n",
224 eal_hpet_resolution_hz/1000);
226 eal_hpet_msb = (eal_hpet->counter_l >> 30);
228 /* create a thread that will increment a global variable for
229 * msb (hpet is 32 bits by default under linux) */
230 ret = pthread_create(&msb_inc_thread_id, NULL,
231 (void *(*)(void *))hpet_msb_inc, NULL);
233 RTE_LOG(ERR, EAL, "ERROR: Cannot create HPET timer thread!\n");
234 internal_config.no_hpet = 1;
239 eal_timer_source = EAL_TIMER_HPET;
245 check_tsc_flags(void)
250 stream = fopen("/proc/cpuinfo", "r");
252 RTE_LOG(WARNING, EAL, "WARNING: Unable to open /proc/cpuinfo\n");
256 while (fgets(line, sizeof line, stream)) {
260 if (strncmp(line, "flags", 5) != 0)
263 constant_tsc = strstr(line, "constant_tsc");
264 nonstop_tsc = strstr(line, "nonstop_tsc");
265 if (!constant_tsc || !nonstop_tsc)
266 RTE_LOG(WARNING, EAL,
267 "WARNING: cpu flags "
270 "-> using unreliable clock cycles !\n",
271 constant_tsc ? "yes":"no",
272 nonstop_tsc ? "yes":"no");
280 set_tsc_freq_from_clock(void)
282 #ifdef CLOCK_MONOTONIC_RAW
283 #define NS_PER_SEC 1E9
285 struct timespec sleeptime = {.tv_nsec = 5E8 }; /* 1/2 second */
287 struct timespec t_start, t_end;
289 if (clock_gettime(CLOCK_MONOTONIC_RAW, &t_start) == 0) {
290 uint64_t ns, end, start = rte_rdtsc();
291 nanosleep(&sleeptime,NULL);
292 clock_gettime(CLOCK_MONOTONIC_RAW, &t_end);
294 ns = ((t_end.tv_sec - t_start.tv_sec) * NS_PER_SEC);
295 ns += (t_end.tv_nsec - t_start.tv_nsec);
297 double secs = (double)ns/NS_PER_SEC;
298 eal_tsc_resolution_hz = (uint64_t)((end - start)/secs);
306 set_tsc_freq_fallback(void)
308 RTE_LOG(WARNING, EAL, "WARNING: clock_gettime cannot use "
309 "CLOCK_MONOTONIC_RAW and HPET is not available"
310 " - clock timings may be less accurate.\n");
311 /* assume that the sleep(1) will sleep for 1 second */
312 uint64_t start = rte_rdtsc();
314 eal_tsc_resolution_hz = rte_rdtsc() - start;
317 * This function measures the TSC frequency. It uses a variety of approaches.
319 * 1. If kernel provides CLOCK_MONOTONIC_RAW we use that to tune the TSC value
320 * 2. If kernel does not provide that, and we have HPET support, tune using HPET
321 * 3. Lastly, if neither of the above can be used, just sleep for 1 second and
322 * tune off that, printing a warning about inaccuracy of timing
327 if (set_tsc_freq_from_clock() < 0)
328 set_tsc_freq_fallback();
330 RTE_LOG(INFO, EAL, "TSC frequency is ~%"PRIu64" KHz\n",
331 eal_tsc_resolution_hz/1000);
335 rte_eal_timer_init(void)
338 eal_timer_source = EAL_TIMER_TSC;