Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation.
3 : : * Copyright(c) 2012-2013 6WIND S.A.
4 : : */
5 : :
6 : : #include <stdio.h>
7 : : #include <stdint.h>
8 : : #include <inttypes.h>
9 : : #ifdef RTE_LIBEAL_USE_HPET
10 : : #include <fcntl.h>
11 : : #include <sys/mman.h>
12 : : #include <unistd.h>
13 : : #endif
14 : :
15 : : #include <rte_common.h>
16 : : #include <rte_cycles.h>
17 : : #include <rte_thread.h>
18 : :
19 : : #include <eal_export.h>
20 : : #include "eal_private.h"
21 : :
22 : : RTE_EXPORT_SYMBOL(eal_timer_source)
23 : : enum timer_source eal_timer_source = EAL_TIMER_HPET;
24 : :
25 : : #ifdef RTE_LIBEAL_USE_HPET
26 : :
27 : : #define DEV_HPET "/dev/hpet"
28 : :
29 : : /* Maximum number of counters. */
30 : : #define HPET_TIMER_NUM 3
31 : :
32 : : /* General capabilities register */
33 : : #define CLK_PERIOD_SHIFT 32 /* Clock period shift. */
34 : : #define CLK_PERIOD_MASK 0xffffffff00000000ULL /* Clock period mask. */
35 : :
36 : : /**
37 : : * HPET timer registers. From the Intel IA-PC HPET (High Precision Event
38 : : * Timers) Specification.
39 : : */
40 : : struct eal_hpet_regs {
41 : : /* Memory-mapped, software visible registers */
42 : : uint64_t capabilities; /**< RO General Capabilities Register. */
43 : : uint64_t reserved0; /**< Reserved for future use. */
44 : : uint64_t config; /**< RW General Configuration Register. */
45 : : uint64_t reserved1; /**< Reserved for future use. */
46 : : uint64_t isr; /**< RW Clear General Interrupt Status. */
47 : : uint64_t reserved2[25]; /**< Reserved for future use. */
48 : : union {
49 : : uint64_t counter; /**< RW Main Counter Value Register. */
50 : : struct {
51 : : uint32_t counter_l; /**< RW Main Counter Low. */
52 : : uint32_t counter_h; /**< RW Main Counter High. */
53 : : };
54 : : };
55 : : uint64_t reserved3; /**< Reserved for future use. */
56 : : struct {
57 : : uint64_t config; /**< RW Timer Config and Capability Reg. */
58 : : uint64_t comp; /**< RW Timer Comparator Value Register. */
59 : : uint64_t fsb; /**< RW FSB Interrupt Route Register. */
60 : : uint64_t reserved4; /**< Reserved for future use. */
61 : : } timers[HPET_TIMER_NUM]; /**< Set of HPET timers. */
62 : : };
63 : :
64 : : /* Mmap'd hpet registers */
65 : : static volatile struct eal_hpet_regs *eal_hpet = NULL;
66 : :
67 : : /* Period at which the HPET counter increments in
68 : : * femtoseconds (10^-15 seconds). */
69 : : static uint32_t eal_hpet_resolution_fs = 0;
70 : :
71 : : /* Frequency of the HPET counter in Hz */
72 : : static uint64_t eal_hpet_resolution_hz = 0;
73 : :
74 : : /* Incremented 4 times during one 32bits hpet full count */
75 : : static uint32_t eal_hpet_msb;
76 : :
77 : : static rte_thread_t msb_inc_thread_id;
78 : :
79 : : /*
80 : : * This function runs on a specific thread to update a global variable
81 : : * containing used to process MSB of the HPET (unfortunately, we need
82 : : * this because hpet is 32 bits by default under linux).
83 : : */
84 : : static uint32_t
85 : : hpet_msb_inc(__rte_unused void *arg)
86 : : {
87 : : uint32_t t;
88 : :
89 : : while (1) {
90 : : t = (eal_hpet->counter_l >> 30);
91 : : if (t != (eal_hpet_msb & 3))
92 : : eal_hpet_msb ++;
93 : : sleep(10);
94 : : }
95 : : return 0;
96 : : }
97 : :
98 : : RTE_EXPORT_SYMBOL(rte_get_hpet_hz)
99 : : uint64_t
100 : : rte_get_hpet_hz(void)
101 : : {
102 : : const struct internal_config *internal_conf =
103 : : eal_get_internal_configuration();
104 : :
105 : : if (internal_conf->no_hpet)
106 : : rte_panic("Error, HPET called, but no HPET present\n");
107 : :
108 : : return eal_hpet_resolution_hz;
109 : : }
110 : :
111 : : RTE_EXPORT_SYMBOL(rte_get_hpet_cycles)
112 : : uint64_t
113 : : rte_get_hpet_cycles(void)
114 : : {
115 : : uint32_t t, msb;
116 : : uint64_t ret;
117 : : const struct internal_config *internal_conf =
118 : : eal_get_internal_configuration();
119 : :
120 : : if (internal_conf->no_hpet)
121 : : rte_panic("Error, HPET called, but no HPET present\n");
122 : :
123 : : t = eal_hpet->counter_l;
124 : : msb = eal_hpet_msb;
125 : : ret = (msb + 2 - (t >> 30)) / 4;
126 : : ret <<= 32;
127 : : ret += t;
128 : : return ret;
129 : : }
130 : :
131 : : #endif
132 : :
133 : : #ifdef RTE_LIBEAL_USE_HPET
134 : : /*
135 : : * Open and mmap /dev/hpet (high precision event timer) that will
136 : : * provide our time reference.
137 : : */
138 : : RTE_EXPORT_SYMBOL(rte_eal_hpet_init)
139 : : int
140 : : rte_eal_hpet_init(int make_default)
141 : : {
142 : : int fd, ret;
143 : : struct internal_config *internal_conf =
144 : : eal_get_internal_configuration();
145 : :
146 : : if (internal_conf->no_hpet) {
147 : : EAL_LOG(NOTICE, "HPET is disabled");
148 : : return -1;
149 : : }
150 : :
151 : : fd = open(DEV_HPET, O_RDONLY);
152 : : if (fd < 0) {
153 : : EAL_LOG(ERR, "ERROR: Cannot open "DEV_HPET": %s!",
154 : : strerror(errno));
155 : : internal_conf->no_hpet = 1;
156 : : return -1;
157 : : }
158 : : eal_hpet = mmap(NULL, 1024, PROT_READ, MAP_SHARED, fd, 0);
159 : : if (eal_hpet == MAP_FAILED) {
160 : : EAL_LOG(ERR, "ERROR: Cannot mmap "DEV_HPET"!");
161 : : close(fd);
162 : : internal_conf->no_hpet = 1;
163 : : return -1;
164 : : }
165 : : close(fd);
166 : :
167 : : eal_hpet_resolution_fs = (uint32_t)((eal_hpet->capabilities &
168 : : CLK_PERIOD_MASK) >>
169 : : CLK_PERIOD_SHIFT);
170 : :
171 : : eal_hpet_resolution_hz = (1000ULL*1000ULL*1000ULL*1000ULL*1000ULL) /
172 : : (uint64_t)eal_hpet_resolution_fs;
173 : :
174 : : EAL_LOG(INFO, "HPET frequency is ~%"PRIu64" kHz",
175 : : eal_hpet_resolution_hz/1000);
176 : :
177 : : eal_hpet_msb = (eal_hpet->counter_l >> 30);
178 : :
179 : : /* create a thread that will increment a global variable for
180 : : * msb (hpet is 32 bits by default under linux) */
181 : : ret = rte_thread_create_internal_control(&msb_inc_thread_id, "hpet-msb",
182 : : hpet_msb_inc, NULL);
183 : : if (ret != 0) {
184 : : EAL_LOG(ERR, "ERROR: Cannot create HPET timer thread!");
185 : : internal_conf->no_hpet = 1;
186 : : return -1;
187 : : }
188 : :
189 : : if (make_default)
190 : : eal_timer_source = EAL_TIMER_HPET;
191 : : return 0;
192 : : }
193 : : #endif
194 : :
195 : : /* Check if the kernel deems the arch provided TSC frequency trustworthy. */
196 : :
197 : : static bool
198 : 0 : is_tsc_known_freq(void)
199 : : {
200 : : bool ret = true; /* Assume tsc_known_freq */
201 : :
202 : : #if defined(RTE_ARCH_X86)
203 : : char line[2048];
204 : : FILE *stream;
205 : :
206 : 0 : stream = fopen("/proc/cpuinfo", "r");
207 [ # # ]: 0 : if (!stream) {
208 : 0 : EAL_LOG(WARNING, "Unable to open /proc/cpuinfo");
209 : 0 : return ret;
210 : : }
211 : :
212 [ # # ]: 0 : while (fgets(line, sizeof(line), stream)) {
213 [ # # ]: 0 : if (strncmp(line, "flags", 5) != 0)
214 : 0 : continue;
215 : :
216 [ # # ]: 0 : if (!strstr(line, "tsc_known_freq"))
217 : : ret = false;
218 : :
219 : : break;
220 : : }
221 : :
222 : 0 : fclose(stream);
223 : : #endif
224 : :
225 : 0 : return ret;
226 : : }
227 : :
228 : : uint64_t
229 : 155 : get_tsc_freq(uint64_t arch_hz)
230 : : {
231 : : #ifdef CLOCK_MONOTONIC_RAW
232 : : #define NS_PER_SEC 1E9
233 : : #define CYC_PER_100KHZ 1E5
234 : :
235 : 155 : struct timespec sleeptime = {.tv_nsec = NS_PER_SEC / 10 }; /* 1/10 second */
236 : :
237 : : struct timespec t_start, t_end;
238 : : uint64_t tsc_hz;
239 : :
240 [ - + - - ]: 155 : if (arch_hz && is_tsc_known_freq())
241 : : return arch_hz;
242 : :
243 [ + - ]: 155 : if (clock_gettime(CLOCK_MONOTONIC_RAW, &t_start) == 0) {
244 : : uint64_t ns, end, start = rte_rdtsc();
245 : 155 : nanosleep(&sleeptime,NULL);
246 : 155 : clock_gettime(CLOCK_MONOTONIC_RAW, &t_end);
247 : : end = rte_rdtsc();
248 : 155 : ns = ((t_end.tv_sec - t_start.tv_sec) * NS_PER_SEC);
249 : 155 : ns += (t_end.tv_nsec - t_start.tv_nsec);
250 : :
251 : 155 : double secs = (double)ns/NS_PER_SEC;
252 : 155 : tsc_hz = (uint64_t)((end - start)/secs);
253 : :
254 [ - + ]: 155 : if (arch_hz) {
255 : : /* Make sure we're within 1% for sanity check */
256 [ # # ]: 0 : if (RTE_MAX(arch_hz, tsc_hz) - RTE_MIN(arch_hz, tsc_hz) > arch_hz / 100)
257 : : return arch_hz;
258 : :
259 : 0 : EAL_LOG(DEBUG,
260 : : "Refined arch frequency %"PRIu64" to measured frequency %"PRIu64,
261 : : arch_hz, tsc_hz);
262 : : }
263 : :
264 : : /* Round up to 100Khz. 1E5 ~ 100Khz */
265 [ + - ]: 310 : return RTE_ALIGN_MUL_NEAR(tsc_hz, CYC_PER_100KHZ);
266 : : }
267 : : #endif
268 : : return arch_hz;
269 : : }
270 : :
271 : : int
272 : 180 : rte_eal_timer_init(void)
273 : : {
274 : :
275 : 180 : eal_timer_source = EAL_TIMER_TSC;
276 : :
277 : 180 : set_tsc_freq();
278 : 180 : return 0;
279 : : }
|