eal: use SPDX tags in 6WIND copyrighted files
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_timer.c
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 <string.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <inttypes.h>
13 #include <sys/mman.h>
14 #include <sys/queue.h>
15 #include <pthread.h>
16 #include <errno.h>
17
18 #include <rte_common.h>
19 #include <rte_log.h>
20 #include <rte_cycles.h>
21 #include <rte_lcore.h>
22 #include <rte_memory.h>
23 #include <rte_eal.h>
24 #include <rte_debug.h>
25
26 #include "eal_private.h"
27 #include "eal_internal_cfg.h"
28
29 enum timer_source eal_timer_source = EAL_TIMER_HPET;
30
31 #ifdef RTE_LIBEAL_USE_HPET
32
33 #define DEV_HPET "/dev/hpet"
34
35 /* Maximum number of counters. */
36 #define HPET_TIMER_NUM 3
37
38 /* General capabilities register */
39 #define CLK_PERIOD_SHIFT     32 /* Clock period shift. */
40 #define CLK_PERIOD_MASK      0xffffffff00000000ULL /* Clock period mask. */
41
42 /**
43  * HPET timer registers. From the Intel IA-PC HPET (High Precision Event
44  * Timers) Specification.
45  */
46 struct eal_hpet_regs {
47         /* Memory-mapped, software visible registers */
48         uint64_t capabilities;      /**< RO General Capabilities Register. */
49         uint64_t reserved0;         /**< Reserved for future use. */
50         uint64_t config;            /**< RW General Configuration Register. */
51         uint64_t reserved1;         /**< Reserved for future use. */
52         uint64_t isr;               /**< RW Clear General Interrupt Status. */
53         uint64_t reserved2[25];     /**< Reserved for future use. */
54         union {
55                 uint64_t counter;   /**< RW Main Counter Value Register. */
56                 struct {
57                         uint32_t counter_l; /**< RW Main Counter Low. */
58                         uint32_t counter_h; /**< RW Main Counter High. */
59                 };
60         };
61         uint64_t reserved3;         /**< Reserved for future use. */
62         struct {
63                 uint64_t config;    /**< RW Timer Config and Capability Reg. */
64                 uint64_t comp;      /**< RW Timer Comparator Value Register. */
65                 uint64_t fsb;       /**< RW FSB Interrupt Route Register. */
66                 uint64_t reserved4; /**< Reserved for future use. */
67         } timers[HPET_TIMER_NUM]; /**< Set of HPET timers. */
68 };
69
70 /* Mmap'd hpet registers */
71 static volatile struct eal_hpet_regs *eal_hpet = NULL;
72
73 /* Period at which the HPET counter increments in
74  * femtoseconds (10^-15 seconds). */
75 static uint32_t eal_hpet_resolution_fs = 0;
76
77 /* Frequency of the HPET counter in Hz */
78 static uint64_t eal_hpet_resolution_hz = 0;
79
80 /* Incremented 4 times during one 32bits hpet full count */
81 static uint32_t eal_hpet_msb;
82
83 static pthread_t msb_inc_thread_id;
84
85 /*
86  * This function runs on a specific thread to update a global variable
87  * containing used to process MSB of the HPET (unfortunately, we need
88  * this because hpet is 32 bits by default under linux).
89  */
90 static void
91 hpet_msb_inc(__attribute__((unused)) void *arg)
92 {
93         uint32_t t;
94
95         while (1) {
96                 t = (eal_hpet->counter_l >> 30);
97                 if (t != (eal_hpet_msb & 3))
98                         eal_hpet_msb ++;
99                 sleep(10);
100         }
101 }
102
103 uint64_t
104 rte_get_hpet_hz(void)
105 {
106         if(internal_config.no_hpet)
107                 rte_panic("Error, HPET called, but no HPET present\n");
108
109         return eal_hpet_resolution_hz;
110 }
111
112 uint64_t
113 rte_get_hpet_cycles(void)
114 {
115         uint32_t t, msb;
116         uint64_t ret;
117
118         if(internal_config.no_hpet)
119                 rte_panic("Error, HPET called, but no HPET present\n");
120
121         t = eal_hpet->counter_l;
122         msb = eal_hpet_msb;
123         ret = (msb + 2 - (t >> 30)) / 4;
124         ret <<= 32;
125         ret += t;
126         return ret;
127 }
128
129 #endif
130
131 #ifdef RTE_LIBEAL_USE_HPET
132 /*
133  * Open and mmap /dev/hpet (high precision event timer) that will
134  * provide our time reference.
135  */
136 int
137 rte_eal_hpet_init(int make_default)
138 {
139         int fd, ret;
140         char thread_name[RTE_MAX_THREAD_NAME_LEN];
141
142         if (internal_config.no_hpet) {
143                 RTE_LOG(NOTICE, EAL, "HPET is disabled\n");
144                 return -1;
145         }
146
147         fd = open(DEV_HPET, O_RDONLY);
148         if (fd < 0) {
149                 RTE_LOG(ERR, EAL, "ERROR: Cannot open "DEV_HPET": %s!\n",
150                         strerror(errno));
151                 internal_config.no_hpet = 1;
152                 return -1;
153         }
154         eal_hpet = mmap(NULL, 1024, PROT_READ, MAP_SHARED, fd, 0);
155         if (eal_hpet == MAP_FAILED) {
156                 RTE_LOG(ERR, EAL, "ERROR: Cannot mmap "DEV_HPET"!\n"
157                                 "Please enable CONFIG_HPET_MMAP in your kernel configuration "
158                                 "to allow HPET support.\n"
159                                 "To run without using HPET, set CONFIG_RTE_LIBEAL_USE_HPET=n "
160                                 "in your build configuration or use '--no-hpet' EAL flag.\n");
161                 close(fd);
162                 internal_config.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         RTE_LOG(INFO, EAL, "HPET frequency is ~%"PRIu64" kHz\n",
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 = pthread_create(&msb_inc_thread_id, NULL,
182                         (void *(*)(void *))hpet_msb_inc, NULL);
183         if (ret != 0) {
184                 RTE_LOG(ERR, EAL, "ERROR: Cannot create HPET timer thread!\n");
185                 internal_config.no_hpet = 1;
186                 return -1;
187         }
188
189         /*
190          * Set thread_name for aid in debugging.
191          */
192         snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "hpet-msb-inc");
193         ret = rte_thread_setname(msb_inc_thread_id, thread_name);
194         if (ret != 0)
195                 RTE_LOG(DEBUG, EAL,
196                         "Cannot set HPET timer thread name!\n");
197
198         if (make_default)
199                 eal_timer_source = EAL_TIMER_HPET;
200         return 0;
201 }
202 #endif
203
204 static void
205 check_tsc_flags(void)
206 {
207         char line[512];
208         FILE *stream;
209
210         stream = fopen("/proc/cpuinfo", "r");
211         if (!stream) {
212                 RTE_LOG(WARNING, EAL, "WARNING: Unable to open /proc/cpuinfo\n");
213                 return;
214         }
215
216         while (fgets(line, sizeof line, stream)) {
217                 char *constant_tsc;
218                 char *nonstop_tsc;
219
220                 if (strncmp(line, "flags", 5) != 0)
221                         continue;
222
223                 constant_tsc = strstr(line, "constant_tsc");
224                 nonstop_tsc = strstr(line, "nonstop_tsc");
225                 if (!constant_tsc || !nonstop_tsc)
226                         RTE_LOG(WARNING, EAL,
227                                 "WARNING: cpu flags "
228                                 "constant_tsc=%s "
229                                 "nonstop_tsc=%s "
230                                 "-> using unreliable clock cycles !\n",
231                                 constant_tsc ? "yes":"no",
232                                 nonstop_tsc ? "yes":"no");
233                 break;
234         }
235
236         fclose(stream);
237 }
238
239 uint64_t
240 get_tsc_freq(void)
241 {
242 #ifdef CLOCK_MONOTONIC_RAW
243 #define NS_PER_SEC 1E9
244
245         struct timespec sleeptime = {.tv_nsec = NS_PER_SEC / 10 }; /* 1/10 second */
246
247         struct timespec t_start, t_end;
248         uint64_t tsc_hz;
249
250         if (clock_gettime(CLOCK_MONOTONIC_RAW, &t_start) == 0) {
251                 uint64_t ns, end, start = rte_rdtsc();
252                 nanosleep(&sleeptime,NULL);
253                 clock_gettime(CLOCK_MONOTONIC_RAW, &t_end);
254                 end = rte_rdtsc();
255                 ns = ((t_end.tv_sec - t_start.tv_sec) * NS_PER_SEC);
256                 ns += (t_end.tv_nsec - t_start.tv_nsec);
257
258                 double secs = (double)ns/NS_PER_SEC;
259                 tsc_hz = (uint64_t)((end - start)/secs);
260                 return tsc_hz;
261         }
262 #endif
263         return 0;
264 }
265
266 int
267 rte_eal_timer_init(void)
268 {
269
270         eal_timer_source = EAL_TIMER_TSC;
271
272         set_tsc_freq();
273         check_tsc_flags();
274         return 0;
275 }