eal: remove unused macros
[dpdk.git] / lib / librte_eal / bsdapp / eal / eal_timer.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <inttypes.h>
41 #include <sys/mman.h>
42 #include <sys/queue.h>
43 #include <pthread.h>
44 #include <errno.h>
45
46 #include <rte_common.h>
47 #include <rte_log.h>
48 #include <rte_cycles.h>
49 #include <rte_tailq.h>
50 #include <rte_memory.h>
51 #include <rte_memzone.h>
52 #include <rte_eal.h>
53 #include <rte_debug.h>
54
55 #include "eal_private.h"
56 #include "eal_internal_cfg.h"
57
58 enum timer_source eal_timer_source = EAL_TIMER_HPET;
59
60 /* The frequency of the RDTSC timer resolution */
61 static uint64_t eal_tsc_resolution_hz = 0;
62
63 #ifdef RTE_LIBEAL_USE_HPET
64
65 #define DEV_HPET "/dev/hpet"
66
67 /* Maximum number of counters. */
68 #define HPET_TIMER_NUM 3
69
70 /* General capabilities register */
71 #define CLK_PERIOD_SHIFT     32 /* Clock period shift. */
72 #define CLK_PERIOD_MASK      0xffffffff00000000ULL /* Clock period mask. */
73
74 /**
75  * HPET timer registers. From the Intel IA-PC HPET (High Precision Event
76  * Timers) Specification.
77  */
78 struct eal_hpet_regs {
79         /* Memory-mapped, software visible registers */
80         uint64_t capabilities;      /**< RO General Capabilities Register. */
81         uint64_t reserved0;         /**< Reserved for future use. */
82         uint64_t config;            /**< RW General Configuration Register. */
83         uint64_t reserved1;         /**< Reserved for future use. */
84         uint64_t isr;               /**< RW Clear General Interrupt Status. */
85         uint64_t reserved2[25];     /**< Reserved for future use. */
86         union {
87                 uint64_t counter;   /**< RW Main Counter Value Register. */
88                 struct {
89                         uint32_t counter_l; /**< RW Main Counter Low. */
90                         uint32_t counter_h; /**< RW Main Counter High. */
91                 };
92         };
93         uint64_t reserved3;         /**< Reserved for future use. */
94         struct {
95                 uint64_t config;    /**< RW Timer Config and Capability Reg. */
96                 uint64_t comp;      /**< RW Timer Comparator Value Register. */
97                 uint64_t fsb;       /**< RW FSB Interrupt Route Register. */
98                 uint64_t reserved4; /**< Reserved for future use. */
99         } timers[HPET_TIMER_NUM]; /**< Set of HPET timers. */
100 };
101
102 /* Mmap'd hpet registers */
103 static volatile struct eal_hpet_regs *eal_hpet = NULL;
104
105 /* Period at which the HPET counter increments in
106  * femtoseconds (10^-15 seconds). */
107 static uint32_t eal_hpet_resolution_fs = 0;
108
109 /* Frequency of the HPET counter in Hz */
110 static uint64_t eal_hpet_resolution_hz = 0;
111
112 /* Incremented 4 times during one 32bits hpet full count */
113 static uint32_t eal_hpet_msb;
114
115 static pthread_t msb_inc_thread_id;
116
117 /*
118  * This function runs on a specific thread to update a global variable
119  * containing used to process MSB of the HPET (unfortunatelly, we need
120  * this because hpet is 32 bits by default under linux).
121  */
122 static void
123 hpet_msb_inc(__attribute__((unused)) void *arg)
124 {
125         uint32_t t;
126
127         while (1) {
128                 t = (eal_hpet->counter_l >> 30);
129                 if (t != (eal_hpet_msb & 3))
130                         eal_hpet_msb ++;
131                 sleep(10);
132         }
133 }
134
135 uint64_t
136 rte_get_hpet_hz(void)
137 {
138         if(internal_config.no_hpet)
139                 rte_panic("Error, HPET called, but no HPET present\n");
140
141         return eal_hpet_resolution_hz;
142 }
143
144 uint64_t
145 rte_get_hpet_cycles(void)
146 {
147         uint32_t t, msb;
148         uint64_t ret;
149
150         if(internal_config.no_hpet)
151                 rte_panic("Error, HPET called, but no HPET present\n");
152
153         t = eal_hpet->counter_l;
154         msb = eal_hpet_msb;
155         ret = (msb + 2 - (t >> 30)) / 4;
156         ret <<= 32;
157         ret += t;
158         return ret;
159 }
160
161 #endif
162
163
164 void
165 rte_delay_us(unsigned us)
166 {
167         const uint64_t start = rte_get_timer_cycles();
168         const uint64_t ticks = (uint64_t)us * rte_get_timer_hz() / 1E6;
169         while ((rte_get_timer_cycles() - start) < ticks)
170                 rte_pause();
171 }
172
173 uint64_t
174 rte_get_tsc_hz(void)
175 {
176         return eal_tsc_resolution_hz;
177 }
178
179
180 #ifdef RTE_LIBEAL_USE_HPET
181 /*
182  * Open and mmap /dev/hpet (high precision event timer) that will
183  * provide our time reference.
184  */
185 int
186 rte_eal_hpet_init(int make_default)
187 {
188         int fd, ret;
189
190         if (internal_config.no_hpet) {
191                 RTE_LOG(INFO, EAL, "HPET is disabled\n");
192                 return -1;
193         }
194
195         fd = open(DEV_HPET, O_RDONLY);
196         if (fd < 0) {
197                 RTE_LOG(ERR, EAL, "ERROR: Cannot open "DEV_HPET": %s!\n",
198                         strerror(errno));
199                 internal_config.no_hpet = 1;
200                 return -1;
201         }
202         eal_hpet = mmap(NULL, 1024, PROT_READ, MAP_SHARED, fd, 0);
203         if (eal_hpet == MAP_FAILED) {
204                 RTE_LOG(ERR, EAL, "ERROR: Cannot mmap "DEV_HPET"!\n"
205                                 "Please enable CONFIG_HPET_MMAP in your kernel configuration "
206                                 "to allow HPET support.\n"
207                                 "To run without using HPET, set CONFIG_RTE_LIBEAL_USE_HPET=n "
208                                 "in your build configuration or use '--no-hpet' EAL flag.\n");
209                 close(fd);
210                 internal_config.no_hpet = 1;
211                 return -1;
212         }
213         close(fd);
214
215         eal_hpet_resolution_fs = (uint32_t)((eal_hpet->capabilities &
216                                         CLK_PERIOD_MASK) >>
217                                         CLK_PERIOD_SHIFT);
218
219         eal_hpet_resolution_hz = (1000ULL*1000ULL*1000ULL*1000ULL*1000ULL) /
220                 (uint64_t)eal_hpet_resolution_fs;
221
222         RTE_LOG(INFO, EAL, "HPET frequency is ~%"PRIu64" kHz\n",
223                         eal_hpet_resolution_hz/1000);
224
225         eal_hpet_msb = (eal_hpet->counter_l >> 30);
226
227         /* create a thread that will increment a global variable for
228          * msb (hpet is 32 bits by default under linux) */
229         ret = pthread_create(&msb_inc_thread_id, NULL,
230                         (void *(*)(void *))hpet_msb_inc, NULL);
231         if (ret < 0) {
232                 RTE_LOG(ERR, EAL, "ERROR: Cannot create HPET timer thread!\n");
233                 internal_config.no_hpet = 1;
234                 return -1;
235         }
236
237         if (make_default)
238                 eal_timer_source = EAL_TIMER_HPET;
239         return 0;
240 }
241 #endif
242
243 static int
244 set_tsc_freq_from_clock(void)
245 {
246 #ifdef CLOCK_MONOTONIC_RAW
247 #define NS_PER_SEC 1E9
248
249         struct timespec sleeptime = {.tv_nsec = 5E8 }; /* 1/2 second */
250
251         struct timespec t_start, t_end;
252
253         if (clock_gettime(CLOCK_MONOTONIC_RAW, &t_start) == 0) {
254                 uint64_t ns, end, start = rte_rdtsc();
255                 nanosleep(&sleeptime,NULL);
256                 clock_gettime(CLOCK_MONOTONIC_RAW, &t_end);
257                 end = rte_rdtsc();
258                 ns = ((t_end.tv_sec - t_start.tv_sec) * NS_PER_SEC);
259                 ns += (t_end.tv_nsec - t_start.tv_nsec);
260
261                 double secs = (double)ns/NS_PER_SEC;
262                 eal_tsc_resolution_hz = (uint64_t)((end - start)/secs);
263                 return 0;
264         }
265 #endif
266         return -1;
267 }
268
269 static void
270 set_tsc_freq_fallback(void)
271 {
272         RTE_LOG(WARNING, EAL, "WARNING: clock_gettime cannot use "
273                         "CLOCK_MONOTONIC_RAW and HPET is not available"
274                         " - clock timings may be less accurate.\n");
275         /* assume that the sleep(1) will sleep for 1 second */
276         uint64_t start = rte_rdtsc();
277         sleep(1);
278         eal_tsc_resolution_hz = rte_rdtsc() - start;
279 }
280 /*
281  * This function measures the TSC frequency. It uses a variety of approaches.
282  *
283  * 1. If kernel provides CLOCK_MONOTONIC_RAW we use that to tune the TSC value
284  * 2. If kernel does not provide that, and we have HPET support, tune using HPET
285  * 3. Lastly, if neither of the above can be used, just sleep for 1 second and
286  * tune off that, printing a warning about inaccuracy of timing
287  */
288 static void
289 set_tsc_freq(void)
290 {
291         if (set_tsc_freq_from_clock() < 0)
292                 set_tsc_freq_fallback();
293
294         RTE_LOG(INFO, EAL, "TSC frequency is ~%"PRIu64" KHz\n",
295                         eal_tsc_resolution_hz/1000);
296 }
297
298 int
299 rte_eal_timer_init(void)
300 {
301
302         eal_timer_source = EAL_TIMER_TSC;
303
304         set_tsc_freq();
305         return 0;
306 }