1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_common.h>
10 #include <rte_cycles.h>
11 #include <rte_pause.h>
14 #include "eal_private.h"
15 #include "eal_memcfg.h"
17 /* The frequency of the RDTSC timer resolution */
18 static uint64_t eal_tsc_resolution_hz;
20 /* Pointer to user delay function */
21 void (*rte_delay_us)(unsigned int) = NULL;
24 rte_delay_us_block(unsigned int us)
26 const uint64_t start = rte_get_timer_cycles();
27 const uint64_t ticks = (uint64_t)us * rte_get_timer_hz() / 1E6;
28 while ((rte_get_timer_cycles() - start) < ticks)
35 return eal_tsc_resolution_hz;
39 estimate_tsc_freq(void)
41 #define CYC_PER_10MHZ 1E7
42 RTE_LOG(WARNING, EAL, "WARNING: TSC frequency estimated roughly"
43 " - clock timings may be less accurate.\n");
44 /* assume that the rte_delay_us_sleep() will sleep for 1 second */
45 uint64_t start = rte_rdtsc();
46 rte_delay_us_sleep(US_PER_S);
47 /* Round up to 10Mhz. 1E7 ~ 10Mhz */
48 return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ);
54 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
57 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
59 * Just use the primary process calculated TSC rate in any
60 * secondary process. It avoids any unnecessary overhead on
61 * systems where arch-specific frequency detection is not
64 eal_tsc_resolution_hz = mcfg->tsc_hz;
68 freq = get_tsc_freq_arch();
70 freq = get_tsc_freq();
72 freq = estimate_tsc_freq();
74 RTE_LOG(DEBUG, EAL, "TSC frequency is ~%" PRIu64 " KHz\n", freq / 1000);
75 eal_tsc_resolution_hz = freq;
79 void rte_delay_us_callback_register(void (*userfunc)(unsigned int))
81 rte_delay_us = userfunc;
84 RTE_INIT(rte_timer_init)
86 /* set rte_delay_us_block as a delay function */
87 rte_delay_us_callback_register(rte_delay_us_block);