xref: /f-stack/dpdk/lib/librte_eal/linux/eal_timer.c (revision 2d9fd380)
1*2d9fd380Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2*2d9fd380Sjfb8856606  * Copyright(c) 2010-2014 Intel Corporation.
3*2d9fd380Sjfb8856606  * Copyright(c) 2012-2013 6WIND S.A.
4*2d9fd380Sjfb8856606  */
5*2d9fd380Sjfb8856606 
6*2d9fd380Sjfb8856606 #include <string.h>
7*2d9fd380Sjfb8856606 #include <stdlib.h>
8*2d9fd380Sjfb8856606 #include <stdio.h>
9*2d9fd380Sjfb8856606 #include <stdint.h>
10*2d9fd380Sjfb8856606 #include <unistd.h>
11*2d9fd380Sjfb8856606 #include <fcntl.h>
12*2d9fd380Sjfb8856606 #include <inttypes.h>
13*2d9fd380Sjfb8856606 #include <sys/mman.h>
14*2d9fd380Sjfb8856606 #include <sys/queue.h>
15*2d9fd380Sjfb8856606 #include <pthread.h>
16*2d9fd380Sjfb8856606 #include <errno.h>
17*2d9fd380Sjfb8856606 
18*2d9fd380Sjfb8856606 #include <rte_common.h>
19*2d9fd380Sjfb8856606 #include <rte_log.h>
20*2d9fd380Sjfb8856606 #include <rte_cycles.h>
21*2d9fd380Sjfb8856606 #include <rte_lcore.h>
22*2d9fd380Sjfb8856606 #include <rte_memory.h>
23*2d9fd380Sjfb8856606 #include <rte_eal.h>
24*2d9fd380Sjfb8856606 #include <rte_debug.h>
25*2d9fd380Sjfb8856606 
26*2d9fd380Sjfb8856606 #include "eal_private.h"
27*2d9fd380Sjfb8856606 #include "eal_internal_cfg.h"
28*2d9fd380Sjfb8856606 
29*2d9fd380Sjfb8856606 enum timer_source eal_timer_source = EAL_TIMER_HPET;
30*2d9fd380Sjfb8856606 
31*2d9fd380Sjfb8856606 #ifdef RTE_LIBEAL_USE_HPET
32*2d9fd380Sjfb8856606 
33*2d9fd380Sjfb8856606 #define DEV_HPET "/dev/hpet"
34*2d9fd380Sjfb8856606 
35*2d9fd380Sjfb8856606 /* Maximum number of counters. */
36*2d9fd380Sjfb8856606 #define HPET_TIMER_NUM 3
37*2d9fd380Sjfb8856606 
38*2d9fd380Sjfb8856606 /* General capabilities register */
39*2d9fd380Sjfb8856606 #define CLK_PERIOD_SHIFT     32 /* Clock period shift. */
40*2d9fd380Sjfb8856606 #define CLK_PERIOD_MASK      0xffffffff00000000ULL /* Clock period mask. */
41*2d9fd380Sjfb8856606 
42*2d9fd380Sjfb8856606 /**
43*2d9fd380Sjfb8856606  * HPET timer registers. From the Intel IA-PC HPET (High Precision Event
44*2d9fd380Sjfb8856606  * Timers) Specification.
45*2d9fd380Sjfb8856606  */
46*2d9fd380Sjfb8856606 struct eal_hpet_regs {
47*2d9fd380Sjfb8856606 	/* Memory-mapped, software visible registers */
48*2d9fd380Sjfb8856606 	uint64_t capabilities;      /**< RO General Capabilities Register. */
49*2d9fd380Sjfb8856606 	uint64_t reserved0;         /**< Reserved for future use. */
50*2d9fd380Sjfb8856606 	uint64_t config;            /**< RW General Configuration Register. */
51*2d9fd380Sjfb8856606 	uint64_t reserved1;         /**< Reserved for future use. */
52*2d9fd380Sjfb8856606 	uint64_t isr;               /**< RW Clear General Interrupt Status. */
53*2d9fd380Sjfb8856606 	uint64_t reserved2[25];     /**< Reserved for future use. */
54*2d9fd380Sjfb8856606 	union {
55*2d9fd380Sjfb8856606 		uint64_t counter;   /**< RW Main Counter Value Register. */
56*2d9fd380Sjfb8856606 		struct {
57*2d9fd380Sjfb8856606 			uint32_t counter_l; /**< RW Main Counter Low. */
58*2d9fd380Sjfb8856606 			uint32_t counter_h; /**< RW Main Counter High. */
59*2d9fd380Sjfb8856606 		};
60*2d9fd380Sjfb8856606 	};
61*2d9fd380Sjfb8856606 	uint64_t reserved3;         /**< Reserved for future use. */
62*2d9fd380Sjfb8856606 	struct {
63*2d9fd380Sjfb8856606 		uint64_t config;    /**< RW Timer Config and Capability Reg. */
64*2d9fd380Sjfb8856606 		uint64_t comp;      /**< RW Timer Comparator Value Register. */
65*2d9fd380Sjfb8856606 		uint64_t fsb;       /**< RW FSB Interrupt Route Register. */
66*2d9fd380Sjfb8856606 		uint64_t reserved4; /**< Reserved for future use. */
67*2d9fd380Sjfb8856606 	} timers[HPET_TIMER_NUM]; /**< Set of HPET timers. */
68*2d9fd380Sjfb8856606 };
69*2d9fd380Sjfb8856606 
70*2d9fd380Sjfb8856606 /* Mmap'd hpet registers */
71*2d9fd380Sjfb8856606 static volatile struct eal_hpet_regs *eal_hpet = NULL;
72*2d9fd380Sjfb8856606 
73*2d9fd380Sjfb8856606 /* Period at which the HPET counter increments in
74*2d9fd380Sjfb8856606  * femtoseconds (10^-15 seconds). */
75*2d9fd380Sjfb8856606 static uint32_t eal_hpet_resolution_fs = 0;
76*2d9fd380Sjfb8856606 
77*2d9fd380Sjfb8856606 /* Frequency of the HPET counter in Hz */
78*2d9fd380Sjfb8856606 static uint64_t eal_hpet_resolution_hz = 0;
79*2d9fd380Sjfb8856606 
80*2d9fd380Sjfb8856606 /* Incremented 4 times during one 32bits hpet full count */
81*2d9fd380Sjfb8856606 static uint32_t eal_hpet_msb;
82*2d9fd380Sjfb8856606 
83*2d9fd380Sjfb8856606 static pthread_t msb_inc_thread_id;
84*2d9fd380Sjfb8856606 
85*2d9fd380Sjfb8856606 /*
86*2d9fd380Sjfb8856606  * This function runs on a specific thread to update a global variable
87*2d9fd380Sjfb8856606  * containing used to process MSB of the HPET (unfortunately, we need
88*2d9fd380Sjfb8856606  * this because hpet is 32 bits by default under linux).
89*2d9fd380Sjfb8856606  */
90*2d9fd380Sjfb8856606 static void *
hpet_msb_inc(__rte_unused void * arg)91*2d9fd380Sjfb8856606 hpet_msb_inc(__rte_unused void *arg)
92*2d9fd380Sjfb8856606 {
93*2d9fd380Sjfb8856606 	uint32_t t;
94*2d9fd380Sjfb8856606 
95*2d9fd380Sjfb8856606 	while (1) {
96*2d9fd380Sjfb8856606 		t = (eal_hpet->counter_l >> 30);
97*2d9fd380Sjfb8856606 		if (t != (eal_hpet_msb & 3))
98*2d9fd380Sjfb8856606 			eal_hpet_msb ++;
99*2d9fd380Sjfb8856606 		sleep(10);
100*2d9fd380Sjfb8856606 	}
101*2d9fd380Sjfb8856606 	return NULL;
102*2d9fd380Sjfb8856606 }
103*2d9fd380Sjfb8856606 
104*2d9fd380Sjfb8856606 uint64_t
rte_get_hpet_hz(void)105*2d9fd380Sjfb8856606 rte_get_hpet_hz(void)
106*2d9fd380Sjfb8856606 {
107*2d9fd380Sjfb8856606 	const struct internal_config *internal_conf =
108*2d9fd380Sjfb8856606 		eal_get_internal_configuration();
109*2d9fd380Sjfb8856606 
110*2d9fd380Sjfb8856606 	if (internal_conf->no_hpet)
111*2d9fd380Sjfb8856606 		rte_panic("Error, HPET called, but no HPET present\n");
112*2d9fd380Sjfb8856606 
113*2d9fd380Sjfb8856606 	return eal_hpet_resolution_hz;
114*2d9fd380Sjfb8856606 }
115*2d9fd380Sjfb8856606 
116*2d9fd380Sjfb8856606 uint64_t
rte_get_hpet_cycles(void)117*2d9fd380Sjfb8856606 rte_get_hpet_cycles(void)
118*2d9fd380Sjfb8856606 {
119*2d9fd380Sjfb8856606 	uint32_t t, msb;
120*2d9fd380Sjfb8856606 	uint64_t ret;
121*2d9fd380Sjfb8856606 	const struct internal_config *internal_conf =
122*2d9fd380Sjfb8856606 		eal_get_internal_configuration();
123*2d9fd380Sjfb8856606 
124*2d9fd380Sjfb8856606 	if (internal_conf->no_hpet)
125*2d9fd380Sjfb8856606 		rte_panic("Error, HPET called, but no HPET present\n");
126*2d9fd380Sjfb8856606 
127*2d9fd380Sjfb8856606 	t = eal_hpet->counter_l;
128*2d9fd380Sjfb8856606 	msb = eal_hpet_msb;
129*2d9fd380Sjfb8856606 	ret = (msb + 2 - (t >> 30)) / 4;
130*2d9fd380Sjfb8856606 	ret <<= 32;
131*2d9fd380Sjfb8856606 	ret += t;
132*2d9fd380Sjfb8856606 	return ret;
133*2d9fd380Sjfb8856606 }
134*2d9fd380Sjfb8856606 
135*2d9fd380Sjfb8856606 #endif
136*2d9fd380Sjfb8856606 
137*2d9fd380Sjfb8856606 #ifdef RTE_LIBEAL_USE_HPET
138*2d9fd380Sjfb8856606 /*
139*2d9fd380Sjfb8856606  * Open and mmap /dev/hpet (high precision event timer) that will
140*2d9fd380Sjfb8856606  * provide our time reference.
141*2d9fd380Sjfb8856606  */
142*2d9fd380Sjfb8856606 int
rte_eal_hpet_init(int make_default)143*2d9fd380Sjfb8856606 rte_eal_hpet_init(int make_default)
144*2d9fd380Sjfb8856606 {
145*2d9fd380Sjfb8856606 	int fd, ret;
146*2d9fd380Sjfb8856606 	struct internal_config *internal_conf =
147*2d9fd380Sjfb8856606 		eal_get_internal_configuration();
148*2d9fd380Sjfb8856606 
149*2d9fd380Sjfb8856606 	if (internal_conf->no_hpet) {
150*2d9fd380Sjfb8856606 		RTE_LOG(NOTICE, EAL, "HPET is disabled\n");
151*2d9fd380Sjfb8856606 		return -1;
152*2d9fd380Sjfb8856606 	}
153*2d9fd380Sjfb8856606 
154*2d9fd380Sjfb8856606 	fd = open(DEV_HPET, O_RDONLY);
155*2d9fd380Sjfb8856606 	if (fd < 0) {
156*2d9fd380Sjfb8856606 		RTE_LOG(ERR, EAL, "ERROR: Cannot open "DEV_HPET": %s!\n",
157*2d9fd380Sjfb8856606 			strerror(errno));
158*2d9fd380Sjfb8856606 		internal_conf->no_hpet = 1;
159*2d9fd380Sjfb8856606 		return -1;
160*2d9fd380Sjfb8856606 	}
161*2d9fd380Sjfb8856606 	eal_hpet = mmap(NULL, 1024, PROT_READ, MAP_SHARED, fd, 0);
162*2d9fd380Sjfb8856606 	if (eal_hpet == MAP_FAILED) {
163*2d9fd380Sjfb8856606 		RTE_LOG(ERR, EAL, "ERROR: Cannot mmap "DEV_HPET"!\n"
164*2d9fd380Sjfb8856606 				"Please enable CONFIG_HPET_MMAP in your kernel configuration "
165*2d9fd380Sjfb8856606 				"to allow HPET support.\n"
166*2d9fd380Sjfb8856606 				"To run without using HPET, unset RTE_LIBEAL_USE_HPET "
167*2d9fd380Sjfb8856606 				"in your build configuration or use '--no-hpet' EAL flag.\n");
168*2d9fd380Sjfb8856606 		close(fd);
169*2d9fd380Sjfb8856606 		internal_conf->no_hpet = 1;
170*2d9fd380Sjfb8856606 		return -1;
171*2d9fd380Sjfb8856606 	}
172*2d9fd380Sjfb8856606 	close(fd);
173*2d9fd380Sjfb8856606 
174*2d9fd380Sjfb8856606 	eal_hpet_resolution_fs = (uint32_t)((eal_hpet->capabilities &
175*2d9fd380Sjfb8856606 					CLK_PERIOD_MASK) >>
176*2d9fd380Sjfb8856606 					CLK_PERIOD_SHIFT);
177*2d9fd380Sjfb8856606 
178*2d9fd380Sjfb8856606 	eal_hpet_resolution_hz = (1000ULL*1000ULL*1000ULL*1000ULL*1000ULL) /
179*2d9fd380Sjfb8856606 		(uint64_t)eal_hpet_resolution_fs;
180*2d9fd380Sjfb8856606 
181*2d9fd380Sjfb8856606 	RTE_LOG(INFO, EAL, "HPET frequency is ~%"PRIu64" kHz\n",
182*2d9fd380Sjfb8856606 			eal_hpet_resolution_hz/1000);
183*2d9fd380Sjfb8856606 
184*2d9fd380Sjfb8856606 	eal_hpet_msb = (eal_hpet->counter_l >> 30);
185*2d9fd380Sjfb8856606 
186*2d9fd380Sjfb8856606 	/* create a thread that will increment a global variable for
187*2d9fd380Sjfb8856606 	 * msb (hpet is 32 bits by default under linux) */
188*2d9fd380Sjfb8856606 	ret = rte_ctrl_thread_create(&msb_inc_thread_id, "hpet-msb-inc", NULL,
189*2d9fd380Sjfb8856606 				     hpet_msb_inc, NULL);
190*2d9fd380Sjfb8856606 	if (ret != 0) {
191*2d9fd380Sjfb8856606 		RTE_LOG(ERR, EAL, "ERROR: Cannot create HPET timer thread!\n");
192*2d9fd380Sjfb8856606 		internal_conf->no_hpet = 1;
193*2d9fd380Sjfb8856606 		return -1;
194*2d9fd380Sjfb8856606 	}
195*2d9fd380Sjfb8856606 
196*2d9fd380Sjfb8856606 	if (make_default)
197*2d9fd380Sjfb8856606 		eal_timer_source = EAL_TIMER_HPET;
198*2d9fd380Sjfb8856606 	return 0;
199*2d9fd380Sjfb8856606 }
200*2d9fd380Sjfb8856606 #endif
201*2d9fd380Sjfb8856606 
202*2d9fd380Sjfb8856606 uint64_t
get_tsc_freq(void)203*2d9fd380Sjfb8856606 get_tsc_freq(void)
204*2d9fd380Sjfb8856606 {
205*2d9fd380Sjfb8856606 #ifdef CLOCK_MONOTONIC_RAW
206*2d9fd380Sjfb8856606 #define NS_PER_SEC 1E9
207*2d9fd380Sjfb8856606 #define CYC_PER_10MHZ 1E7
208*2d9fd380Sjfb8856606 
209*2d9fd380Sjfb8856606 	struct timespec sleeptime = {.tv_nsec = NS_PER_SEC / 10 }; /* 1/10 second */
210*2d9fd380Sjfb8856606 
211*2d9fd380Sjfb8856606 	struct timespec t_start, t_end;
212*2d9fd380Sjfb8856606 	uint64_t tsc_hz;
213*2d9fd380Sjfb8856606 
214*2d9fd380Sjfb8856606 	if (clock_gettime(CLOCK_MONOTONIC_RAW, &t_start) == 0) {
215*2d9fd380Sjfb8856606 		uint64_t ns, end, start = rte_rdtsc();
216*2d9fd380Sjfb8856606 		nanosleep(&sleeptime,NULL);
217*2d9fd380Sjfb8856606 		clock_gettime(CLOCK_MONOTONIC_RAW, &t_end);
218*2d9fd380Sjfb8856606 		end = rte_rdtsc();
219*2d9fd380Sjfb8856606 		ns = ((t_end.tv_sec - t_start.tv_sec) * NS_PER_SEC);
220*2d9fd380Sjfb8856606 		ns += (t_end.tv_nsec - t_start.tv_nsec);
221*2d9fd380Sjfb8856606 
222*2d9fd380Sjfb8856606 		double secs = (double)ns/NS_PER_SEC;
223*2d9fd380Sjfb8856606 		tsc_hz = (uint64_t)((end - start)/secs);
224*2d9fd380Sjfb8856606 		/* Round up to 10Mhz. 1E7 ~ 10Mhz */
225*2d9fd380Sjfb8856606 		return RTE_ALIGN_MUL_NEAR(tsc_hz, CYC_PER_10MHZ);
226*2d9fd380Sjfb8856606 	}
227*2d9fd380Sjfb8856606 #endif
228*2d9fd380Sjfb8856606 	return 0;
229*2d9fd380Sjfb8856606 }
230*2d9fd380Sjfb8856606 
231*2d9fd380Sjfb8856606 int
rte_eal_timer_init(void)232*2d9fd380Sjfb8856606 rte_eal_timer_init(void)
233*2d9fd380Sjfb8856606 {
234*2d9fd380Sjfb8856606 
235*2d9fd380Sjfb8856606 	eal_timer_source = EAL_TIMER_TSC;
236*2d9fd380Sjfb8856606 
237*2d9fd380Sjfb8856606 	set_tsc_freq();
238*2d9fd380Sjfb8856606 	return 0;
239*2d9fd380Sjfb8856606 }
240