xref: /dpdk/lib/eal/freebsd/eal_timer.c (revision dbdf3d5581caa1de40b5952e41d54b64e39536d1)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson #include <string.h>
599a2dd95SBruce Richardson #include <stdio.h>
699a2dd95SBruce Richardson #include <unistd.h>
799a2dd95SBruce Richardson #include <inttypes.h>
899a2dd95SBruce Richardson #include <sys/types.h>
999a2dd95SBruce Richardson #include <sys/sysctl.h>
1099a2dd95SBruce Richardson #include <errno.h>
1199a2dd95SBruce Richardson 
1299a2dd95SBruce Richardson #include <rte_common.h>
1399a2dd95SBruce Richardson #include <rte_log.h>
1499a2dd95SBruce Richardson #include <rte_cycles.h>
1599a2dd95SBruce Richardson #include <rte_memory.h>
1699a2dd95SBruce Richardson #include <rte_eal.h>
1799a2dd95SBruce Richardson #include <rte_debug.h>
1899a2dd95SBruce Richardson 
1999a2dd95SBruce Richardson #include "eal_private.h"
2099a2dd95SBruce Richardson #include "eal_internal_cfg.h"
2199a2dd95SBruce Richardson 
2299a2dd95SBruce Richardson #ifdef RTE_LIBEAL_USE_HPET
2399a2dd95SBruce Richardson #warning HPET is not supported in FreeBSD
2499a2dd95SBruce Richardson #endif
2599a2dd95SBruce Richardson 
2699a2dd95SBruce Richardson enum timer_source eal_timer_source = EAL_TIMER_TSC;
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson uint64_t
29*dbdf3d55SIsaac Boukris get_tsc_freq(uint64_t arch_hz)
3099a2dd95SBruce Richardson {
3199a2dd95SBruce Richardson 	size_t sz;
3299a2dd95SBruce Richardson 	int tmp;
3399a2dd95SBruce Richardson 	uint64_t tsc_hz;
3499a2dd95SBruce Richardson 
3599a2dd95SBruce Richardson 	sz = sizeof(tmp);
3699a2dd95SBruce Richardson 	tmp = 0;
3799a2dd95SBruce Richardson 
3899a2dd95SBruce Richardson 	if (sysctlbyname("kern.timecounter.smp_tsc", &tmp, &sz, NULL, 0))
39ae67895bSDavid Marchand 		EAL_LOG(WARNING, "%s", strerror(errno));
4099a2dd95SBruce Richardson 	else if (tmp != 1)
41ae67895bSDavid Marchand 		EAL_LOG(WARNING, "TSC is not safe to use in SMP mode");
4299a2dd95SBruce Richardson 
4399a2dd95SBruce Richardson 	tmp = 0;
4499a2dd95SBruce Richardson 
4599a2dd95SBruce Richardson 	if (sysctlbyname("kern.timecounter.invariant_tsc", &tmp, &sz, NULL, 0))
46ae67895bSDavid Marchand 		EAL_LOG(WARNING, "%s", strerror(errno));
4799a2dd95SBruce Richardson 	else if (tmp != 1)
48ae67895bSDavid Marchand 		EAL_LOG(WARNING, "TSC is not invariant");
4999a2dd95SBruce Richardson 
5099a2dd95SBruce Richardson 	sz = sizeof(tsc_hz);
5199a2dd95SBruce Richardson 	if (sysctlbyname("machdep.tsc_freq", &tsc_hz, &sz, NULL, 0)) {
52ae67895bSDavid Marchand 		EAL_LOG(WARNING, "%s", strerror(errno));
53*dbdf3d55SIsaac Boukris 		return arch_hz;
5499a2dd95SBruce Richardson 	}
5599a2dd95SBruce Richardson 
56*dbdf3d55SIsaac Boukris 	if (arch_hz && RTE_MAX(arch_hz, tsc_hz) - RTE_MIN(arch_hz, tsc_hz) > arch_hz / 100)
57*dbdf3d55SIsaac Boukris 		EAL_LOG(WARNING, "Host tsc_freq %"PRIu64" at odds with cpu value %"PRIu64,
58*dbdf3d55SIsaac Boukris 			tsc_hz, arch_hz);
59*dbdf3d55SIsaac Boukris 
6099a2dd95SBruce Richardson 	return tsc_hz;
6199a2dd95SBruce Richardson }
6299a2dd95SBruce Richardson 
6399a2dd95SBruce Richardson int
6499a2dd95SBruce Richardson rte_eal_timer_init(void)
6599a2dd95SBruce Richardson {
6699a2dd95SBruce Richardson 	set_tsc_freq();
6799a2dd95SBruce Richardson 	return 0;
6899a2dd95SBruce Richardson }
69