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_lcore.h>
51 #include <rte_memory.h>
53 #include <rte_debug.h>
55 #include "eal_private.h"
56 #include "eal_internal_cfg.h"
58 enum timer_source eal_timer_source = EAL_TIMER_HPET;
60 #ifdef RTE_LIBEAL_USE_HPET
62 #define DEV_HPET "/dev/hpet"
64 /* Maximum number of counters. */
65 #define HPET_TIMER_NUM 3
67 /* General capabilities register */
68 #define CLK_PERIOD_SHIFT 32 /* Clock period shift. */
69 #define CLK_PERIOD_MASK 0xffffffff00000000ULL /* Clock period mask. */
72 * HPET timer registers. From the Intel IA-PC HPET (High Precision Event
73 * Timers) Specification.
75 struct eal_hpet_regs {
76 /* Memory-mapped, software visible registers */
77 uint64_t capabilities; /**< RO General Capabilities Register. */
78 uint64_t reserved0; /**< Reserved for future use. */
79 uint64_t config; /**< RW General Configuration Register. */
80 uint64_t reserved1; /**< Reserved for future use. */
81 uint64_t isr; /**< RW Clear General Interrupt Status. */
82 uint64_t reserved2[25]; /**< Reserved for future use. */
84 uint64_t counter; /**< RW Main Counter Value Register. */
86 uint32_t counter_l; /**< RW Main Counter Low. */
87 uint32_t counter_h; /**< RW Main Counter High. */
90 uint64_t reserved3; /**< Reserved for future use. */
92 uint64_t config; /**< RW Timer Config and Capability Reg. */
93 uint64_t comp; /**< RW Timer Comparator Value Register. */
94 uint64_t fsb; /**< RW FSB Interrupt Route Register. */
95 uint64_t reserved4; /**< Reserved for future use. */
96 } timers[HPET_TIMER_NUM]; /**< Set of HPET timers. */
99 /* Mmap'd hpet registers */
100 static volatile struct eal_hpet_regs *eal_hpet = NULL;
102 /* Period at which the HPET counter increments in
103 * femtoseconds (10^-15 seconds). */
104 static uint32_t eal_hpet_resolution_fs = 0;
106 /* Frequency of the HPET counter in Hz */
107 static uint64_t eal_hpet_resolution_hz = 0;
109 /* Incremented 4 times during one 32bits hpet full count */
110 static uint32_t eal_hpet_msb;
112 static pthread_t msb_inc_thread_id;
115 * This function runs on a specific thread to update a global variable
116 * containing used to process MSB of the HPET (unfortunately, we need
117 * this because hpet is 32 bits by default under linux).
120 hpet_msb_inc(__attribute__((unused)) void *arg)
125 t = (eal_hpet->counter_l >> 30);
126 if (t != (eal_hpet_msb & 3))
133 rte_get_hpet_hz(void)
135 if(internal_config.no_hpet)
136 rte_panic("Error, HPET called, but no HPET present\n");
138 return eal_hpet_resolution_hz;
142 rte_get_hpet_cycles(void)
147 if(internal_config.no_hpet)
148 rte_panic("Error, HPET called, but no HPET present\n");
150 t = eal_hpet->counter_l;
152 ret = (msb + 2 - (t >> 30)) / 4;
160 #ifdef RTE_LIBEAL_USE_HPET
162 * Open and mmap /dev/hpet (high precision event timer) that will
163 * provide our time reference.
166 rte_eal_hpet_init(int make_default)
169 char thread_name[RTE_MAX_THREAD_NAME_LEN];
171 if (internal_config.no_hpet) {
172 RTE_LOG(NOTICE, EAL, "HPET is disabled\n");
176 fd = open(DEV_HPET, O_RDONLY);
178 RTE_LOG(ERR, EAL, "ERROR: Cannot open "DEV_HPET": %s!\n",
180 internal_config.no_hpet = 1;
183 eal_hpet = mmap(NULL, 1024, PROT_READ, MAP_SHARED, fd, 0);
184 if (eal_hpet == MAP_FAILED) {
185 RTE_LOG(ERR, EAL, "ERROR: Cannot mmap "DEV_HPET"!\n"
186 "Please enable CONFIG_HPET_MMAP in your kernel configuration "
187 "to allow HPET support.\n"
188 "To run without using HPET, set CONFIG_RTE_LIBEAL_USE_HPET=n "
189 "in your build configuration or use '--no-hpet' EAL flag.\n");
191 internal_config.no_hpet = 1;
196 eal_hpet_resolution_fs = (uint32_t)((eal_hpet->capabilities &
200 eal_hpet_resolution_hz = (1000ULL*1000ULL*1000ULL*1000ULL*1000ULL) /
201 (uint64_t)eal_hpet_resolution_fs;
203 RTE_LOG(INFO, EAL, "HPET frequency is ~%"PRIu64" kHz\n",
204 eal_hpet_resolution_hz/1000);
206 eal_hpet_msb = (eal_hpet->counter_l >> 30);
208 /* create a thread that will increment a global variable for
209 * msb (hpet is 32 bits by default under linux) */
210 ret = pthread_create(&msb_inc_thread_id, NULL,
211 (void *(*)(void *))hpet_msb_inc, NULL);
213 RTE_LOG(ERR, EAL, "ERROR: Cannot create HPET timer thread!\n");
214 internal_config.no_hpet = 1;
219 * Set thread_name for aid in debugging.
221 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "hpet-msb-inc");
222 ret = rte_thread_setname(msb_inc_thread_id, thread_name);
225 "Cannot set HPET timer thread name!\n");
228 eal_timer_source = EAL_TIMER_HPET;
234 check_tsc_flags(void)
239 stream = fopen("/proc/cpuinfo", "r");
241 RTE_LOG(WARNING, EAL, "WARNING: Unable to open /proc/cpuinfo\n");
245 while (fgets(line, sizeof line, stream)) {
249 if (strncmp(line, "flags", 5) != 0)
252 constant_tsc = strstr(line, "constant_tsc");
253 nonstop_tsc = strstr(line, "nonstop_tsc");
254 if (!constant_tsc || !nonstop_tsc)
255 RTE_LOG(WARNING, EAL,
256 "WARNING: cpu flags "
259 "-> using unreliable clock cycles !\n",
260 constant_tsc ? "yes":"no",
261 nonstop_tsc ? "yes":"no");
271 #ifdef CLOCK_MONOTONIC_RAW
272 #define NS_PER_SEC 1E9
274 struct timespec sleeptime = {.tv_nsec = NS_PER_SEC / 10 }; /* 1/10 second */
276 struct timespec t_start, t_end;
279 if (clock_gettime(CLOCK_MONOTONIC_RAW, &t_start) == 0) {
280 uint64_t ns, end, start = rte_rdtsc();
281 nanosleep(&sleeptime,NULL);
282 clock_gettime(CLOCK_MONOTONIC_RAW, &t_end);
284 ns = ((t_end.tv_sec - t_start.tv_sec) * NS_PER_SEC);
285 ns += (t_end.tv_nsec - t_start.tv_nsec);
287 double secs = (double)ns/NS_PER_SEC;
288 tsc_hz = (uint64_t)((end - start)/secs);
296 rte_eal_timer_init(void)
299 eal_timer_source = EAL_TIMER_TSC;