]> git.proxmox.com Git - ceph.git/blob - ceph/src/dpdk/lib/librte_eal/linuxapp/eal/eal_timer.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / dpdk / 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_lcore.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 #ifdef RTE_LIBEAL_USE_HPET
62
63 #define DEV_HPET "/dev/hpet"
64
65 /* Maximum number of counters. */
66 #define HPET_TIMER_NUM 3
67
68 /* General capabilities register */
69 #define CLK_PERIOD_SHIFT 32 /* Clock period shift. */
70 #define CLK_PERIOD_MASK 0xffffffff00000000ULL /* Clock period mask. */
71
72 /**
73 * HPET timer registers. From the Intel IA-PC HPET (High Precision Event
74 * Timers) Specification.
75 */
76 struct eal_hpet_regs {
77 /* Memory-mapped, software visible registers */
78 uint64_t capabilities; /**< RO General Capabilities Register. */
79 uint64_t reserved0; /**< Reserved for future use. */
80 uint64_t config; /**< RW General Configuration Register. */
81 uint64_t reserved1; /**< Reserved for future use. */
82 uint64_t isr; /**< RW Clear General Interrupt Status. */
83 uint64_t reserved2[25]; /**< Reserved for future use. */
84 union {
85 uint64_t counter; /**< RW Main Counter Value Register. */
86 struct {
87 uint32_t counter_l; /**< RW Main Counter Low. */
88 uint32_t counter_h; /**< RW Main Counter High. */
89 };
90 };
91 uint64_t reserved3; /**< Reserved for future use. */
92 struct {
93 uint64_t config; /**< RW Timer Config and Capability Reg. */
94 uint64_t comp; /**< RW Timer Comparator Value Register. */
95 uint64_t fsb; /**< RW FSB Interrupt Route Register. */
96 uint64_t reserved4; /**< Reserved for future use. */
97 } timers[HPET_TIMER_NUM]; /**< Set of HPET timers. */
98 };
99
100 /* Mmap'd hpet registers */
101 static volatile struct eal_hpet_regs *eal_hpet = NULL;
102
103 /* Period at which the HPET counter increments in
104 * femtoseconds (10^-15 seconds). */
105 static uint32_t eal_hpet_resolution_fs = 0;
106
107 /* Frequency of the HPET counter in Hz */
108 static uint64_t eal_hpet_resolution_hz = 0;
109
110 /* Incremented 4 times during one 32bits hpet full count */
111 static uint32_t eal_hpet_msb;
112
113 static pthread_t msb_inc_thread_id;
114
115 /*
116 * This function runs on a specific thread to update a global variable
117 * containing used to process MSB of the HPET (unfortunatelly, we need
118 * this because hpet is 32 bits by default under linux).
119 */
120 static void
121 hpet_msb_inc(__attribute__((unused)) void *arg)
122 {
123 uint32_t t;
124
125 while (1) {
126 t = (eal_hpet->counter_l >> 30);
127 if (t != (eal_hpet_msb & 3))
128 eal_hpet_msb ++;
129 sleep(10);
130 }
131 }
132
133 uint64_t
134 rte_get_hpet_hz(void)
135 {
136 if(internal_config.no_hpet)
137 rte_panic("Error, HPET called, but no HPET present\n");
138
139 return eal_hpet_resolution_hz;
140 }
141
142 uint64_t
143 rte_get_hpet_cycles(void)
144 {
145 uint32_t t, msb;
146 uint64_t ret;
147
148 if(internal_config.no_hpet)
149 rte_panic("Error, HPET called, but no HPET present\n");
150
151 t = eal_hpet->counter_l;
152 msb = eal_hpet_msb;
153 ret = (msb + 2 - (t >> 30)) / 4;
154 ret <<= 32;
155 ret += t;
156 return ret;
157 }
158
159 #endif
160
161 #ifdef RTE_LIBEAL_USE_HPET
162 /*
163 * Open and mmap /dev/hpet (high precision event timer) that will
164 * provide our time reference.
165 */
166 int
167 rte_eal_hpet_init(int make_default)
168 {
169 int fd, ret;
170 char thread_name[RTE_MAX_THREAD_NAME_LEN];
171
172 if (internal_config.no_hpet) {
173 RTE_LOG(NOTICE, EAL, "HPET is disabled\n");
174 return -1;
175 }
176
177 fd = open(DEV_HPET, O_RDONLY);
178 if (fd < 0) {
179 RTE_LOG(ERR, EAL, "ERROR: Cannot open "DEV_HPET": %s!\n",
180 strerror(errno));
181 internal_config.no_hpet = 1;
182 return -1;
183 }
184 eal_hpet = mmap(NULL, 1024, PROT_READ, MAP_SHARED, fd, 0);
185 if (eal_hpet == MAP_FAILED) {
186 RTE_LOG(ERR, EAL, "ERROR: Cannot mmap "DEV_HPET"!\n"
187 "Please enable CONFIG_HPET_MMAP in your kernel configuration "
188 "to allow HPET support.\n"
189 "To run without using HPET, set CONFIG_RTE_LIBEAL_USE_HPET=n "
190 "in your build configuration or use '--no-hpet' EAL flag.\n");
191 close(fd);
192 internal_config.no_hpet = 1;
193 return -1;
194 }
195 close(fd);
196
197 eal_hpet_resolution_fs = (uint32_t)((eal_hpet->capabilities &
198 CLK_PERIOD_MASK) >>
199 CLK_PERIOD_SHIFT);
200
201 eal_hpet_resolution_hz = (1000ULL*1000ULL*1000ULL*1000ULL*1000ULL) /
202 (uint64_t)eal_hpet_resolution_fs;
203
204 RTE_LOG(INFO, EAL, "HPET frequency is ~%"PRIu64" kHz\n",
205 eal_hpet_resolution_hz/1000);
206
207 eal_hpet_msb = (eal_hpet->counter_l >> 30);
208
209 /* create a thread that will increment a global variable for
210 * msb (hpet is 32 bits by default under linux) */
211 ret = pthread_create(&msb_inc_thread_id, NULL,
212 (void *(*)(void *))hpet_msb_inc, NULL);
213 if (ret != 0) {
214 RTE_LOG(ERR, EAL, "ERROR: Cannot create HPET timer thread!\n");
215 internal_config.no_hpet = 1;
216 return -1;
217 }
218
219 /*
220 * Set thread_name for aid in debugging.
221 */
222 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "hpet-msb-inc");
223 ret = rte_thread_setname(msb_inc_thread_id, thread_name);
224 if (ret != 0)
225 RTE_LOG(DEBUG, EAL,
226 "Cannot set HPET timer thread name!\n");
227
228 if (make_default)
229 eal_timer_source = EAL_TIMER_HPET;
230 return 0;
231 }
232 #endif
233
234 static void
235 check_tsc_flags(void)
236 {
237 char line[512];
238 FILE *stream;
239
240 stream = fopen("/proc/cpuinfo", "r");
241 if (!stream) {
242 RTE_LOG(WARNING, EAL, "WARNING: Unable to open /proc/cpuinfo\n");
243 return;
244 }
245
246 while (fgets(line, sizeof line, stream)) {
247 char *constant_tsc;
248 char *nonstop_tsc;
249
250 if (strncmp(line, "flags", 5) != 0)
251 continue;
252
253 constant_tsc = strstr(line, "constant_tsc");
254 nonstop_tsc = strstr(line, "nonstop_tsc");
255 if (!constant_tsc || !nonstop_tsc)
256 RTE_LOG(WARNING, EAL,
257 "WARNING: cpu flags "
258 "constant_tsc=%s "
259 "nonstop_tsc=%s "
260 "-> using unreliable clock cycles !\n",
261 constant_tsc ? "yes":"no",
262 nonstop_tsc ? "yes":"no");
263 break;
264 }
265
266 fclose(stream);
267 }
268
269 uint64_t
270 get_tsc_freq(void)
271 {
272 #ifdef CLOCK_MONOTONIC_RAW
273 #define NS_PER_SEC 1E9
274
275 struct timespec sleeptime = {.tv_nsec = NS_PER_SEC / 10 }; /* 1/10 second */
276
277 struct timespec t_start, t_end;
278 uint64_t tsc_hz;
279
280 if (clock_gettime(CLOCK_MONOTONIC_RAW, &t_start) == 0) {
281 uint64_t ns, end, start = rte_rdtsc();
282 nanosleep(&sleeptime,NULL);
283 clock_gettime(CLOCK_MONOTONIC_RAW, &t_end);
284 end = rte_rdtsc();
285 ns = ((t_end.tv_sec - t_start.tv_sec) * NS_PER_SEC);
286 ns += (t_end.tv_nsec - t_start.tv_nsec);
287
288 double secs = (double)ns/NS_PER_SEC;
289 tsc_hz = (uint64_t)((end - start)/secs);
290 return tsc_hz;
291 }
292 #endif
293 return 0;
294 }
295
296 int
297 rte_eal_timer_init(void)
298 {
299
300 eal_timer_source = EAL_TIMER_TSC;
301
302 set_tsc_freq();
303 check_tsc_flags();
304 return 0;
305 }