4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
43 #include <sys/queue.h>
48 #include <rte_common.h>
50 #include <rte_cycles.h>
51 #include <rte_tailq.h>
52 #include <rte_memory.h>
53 #include <rte_memzone.h>
56 #include "eal_private.h"
57 #include "eal_internal_cfg.h"
59 #define DEV_HPET "/dev/hpet"
61 /* Maximum number of counters. */
62 #define HPET_TIMER_NUM 3
64 /* General capabilities register */
65 #define CLK_PERIOD_SHIFT 32 /* Clock period shift. */
66 #define CLK_PERIOD_MASK 0xffffffff00000000ULL /* Clock period mask. */
67 #define COUNT_SIZE_CAP_SHIFT 13 /* Count size capa. shift. */
68 #define COUNT_SIZE_CAP_MASK 0x0000000000002000ULL /* Count size capa. mask. */
71 * HPET timer registers. From the Intel IA-PC HPET (High Precision Event
72 * Timers) Specification.
74 struct eal_hpet_regs {
75 /* Memory-mapped, software visible registers */
76 uint64_t capabilities; /**< RO General Capabilities Register. */
77 uint64_t reserved0; /**< Reserved for future use. */
78 uint64_t config; /**< RW General Configuration Register. */
79 uint64_t reserved1; /**< Reserved for future use. */
80 uint64_t isr; /**< RW Clear General Interrupt Status. */
81 uint64_t reserved2[25]; /**< Reserved for future use. */
83 uint64_t counter; /**< RW Main Counter Value Register. */
85 uint32_t counter_l; /**< RW Main Counter Low. */
86 uint32_t counter_h; /**< RW Main Counter High. */
89 uint64_t reserved3; /**< Reserved for future use. */
91 uint64_t config; /**< RW Timer Config and Capability Reg. */
92 uint64_t comp; /**< RW Timer Comparator Value Register. */
93 uint64_t fsb; /**< RW FSB Interrupt Route Register. */
94 uint64_t reserved4; /**< Reserved for future use. */
95 } timers[HPET_TIMER_NUM]; /**< Set of HPET timers. */
98 /* Mmap'd hpet registers */
99 static volatile struct eal_hpet_regs *eal_hpet = NULL;
101 /* Period at which the counter increments in femtoseconds (10^-15 seconds). */
102 static uint32_t eal_hpet_resolution_fs = 0;
104 /* Frequency of the counter in Hz */
105 static uint64_t eal_hpet_resolution_hz = 0;
107 /* Incremented 4 times during one 32bits hpet full count */
108 static uint32_t eal_hpet_msb;
110 static pthread_t msb_inc_thread_id;
113 * This function runs on a specific thread to update a global variable
114 * containing used to process MSB of the HPET (unfortunatelly, we need
115 * this because hpet is 32 bits by default under linux).
117 static __attribute__((noreturn)) void *
118 hpet_msb_inc(__attribute__((unused)) void *arg)
123 t = (eal_hpet->counter_l >> 30);
124 if (t != (eal_hpet_msb & 3))
137 eal_hpet_resolution_hz = rte_rdtsc() - start;
138 eal_hpet_resolution_fs = (uint32_t)
139 ((1.0 / eal_hpet_resolution_hz) / 1e-15);
143 * Open and mmap /dev/hpet (high precision event timer) that will
144 * provide our time reference.
147 rte_eal_hpet_init(void)
151 if (internal_config.no_hpet) {
155 fd = open(DEV_HPET, O_RDONLY);
157 RTE_LOG(WARNING, EAL, "WARNING: Cannot open "DEV_HPET": %s! "
158 "The TSC will be used instead.\n",
162 eal_hpet = mmap(NULL, 1024, PROT_READ, MAP_SHARED, fd, 0);
163 if (eal_hpet == MAP_FAILED) {
164 RTE_LOG(WARNING, EAL, "WARNING: Cannot mmap "DEV_HPET"! "
165 "The TSC will be used instead.\n");
171 eal_hpet_resolution_fs = (uint32_t)((eal_hpet->capabilities &
175 eal_hpet_resolution_hz = (1000ULL*1000ULL*1000ULL*1000ULL*1000ULL) /
176 (uint64_t)eal_hpet_resolution_fs;
178 eal_hpet_msb = (eal_hpet->counter_l >> 30);
180 /* create a thread that will increment a global variable for
181 * msb (hpet is 32 bits by default under linux) */
182 ret = pthread_create(&msb_inc_thread_id, NULL, hpet_msb_inc, NULL);
184 RTE_LOG(WARNING, EAL, "WARNING: Cannot create HPET timer thread! "
185 "The TSC will be used instead.\n");
192 internal_config.no_hpet = 1;
198 rte_get_hpet_hz(void)
200 return eal_hpet_resolution_hz;
204 rte_get_hpet_cycles(void)
209 if(internal_config.no_hpet)
210 /* fallback to rdtsc */
213 t = eal_hpet->counter_l;
215 ret = (msb + 2 - (t >> 30)) / 4;
222 rte_delay_us(unsigned us)
226 ticks = (uint64_t)us * 1000ULL * 1000ULL * 1000ULL;
227 ticks /= eal_hpet_resolution_fs;
228 start = rte_get_hpet_cycles();
229 while ((rte_get_hpet_cycles() - start) < ticks)