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