1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2020 Mellanox Technologies, Ltd 3 */ 4 5 #include <inttypes.h> 6 7 #include <rte_windows.h> 8 #include <rte_common.h> 9 #include <rte_cycles.h> 10 #include <rte_eal.h> 11 #include <rte_errno.h> 12 #include "eal_private.h" 13 14 #define US_PER_SEC 1E6 15 #define CYC_PER_100KHZ 1E5 16 17 void 18 rte_delay_us_sleep(unsigned int us) 19 { 20 HANDLE timer; 21 LARGE_INTEGER due_time; 22 23 /* create waitable timer */ 24 timer = CreateWaitableTimer(NULL, TRUE, NULL); 25 if (!timer) { 26 RTE_LOG_WIN32_ERR("CreateWaitableTimer()"); 27 rte_errno = ENOMEM; 28 return; 29 } 30 31 /* 32 * due_time's uom is 100 ns, multiply by 10 to convert to microseconds 33 * set us microseconds time for timer 34 */ 35 due_time.QuadPart = -((int64_t)us * 10); 36 if (!SetWaitableTimer(timer, &due_time, 0, NULL, NULL, FALSE)) { 37 RTE_LOG_WIN32_ERR("SetWaitableTimer()"); 38 rte_errno = EINVAL; 39 goto end; 40 } 41 /* start wait for timer for us microseconds */ 42 if (WaitForSingleObject(timer, INFINITE) == WAIT_FAILED) { 43 RTE_LOG_WIN32_ERR("WaitForSingleObject()"); 44 rte_errno = EINVAL; 45 } 46 47 end: 48 CloseHandle(timer); 49 } 50 51 uint64_t 52 get_tsc_freq(uint64_t arch_hz) 53 { 54 LARGE_INTEGER t_start, t_end, elapsed_us; 55 LARGE_INTEGER frequency; 56 uint64_t tsc_hz; 57 uint64_t end, start; 58 59 if (arch_hz) 60 return arch_hz; 61 62 QueryPerformanceFrequency(&frequency); 63 64 QueryPerformanceCounter(&t_start); 65 start = rte_get_tsc_cycles(); 66 67 rte_delay_us_sleep(US_PER_SEC / 10); /* 1/10 second */ 68 69 if (rte_errno != 0) 70 return 0; 71 72 QueryPerformanceCounter(&t_end); 73 end = rte_get_tsc_cycles(); 74 75 elapsed_us.QuadPart = t_end.QuadPart - t_start.QuadPart; 76 77 /* 78 * To guard against loss-of-precision, convert to microseconds 79 * *before* dividing by ticks-per-second. 80 */ 81 elapsed_us.QuadPart *= US_PER_SEC; 82 elapsed_us.QuadPart /= frequency.QuadPart; 83 84 double secs = ((double)elapsed_us.QuadPart)/US_PER_SEC; 85 tsc_hz = (uint64_t)((end - start)/secs); 86 87 /* Round up to 100Khz. 1E5 ~ 100Khz */ 88 return RTE_ALIGN_MUL_NEAR(tsc_hz, CYC_PER_100KHZ); 89 } 90 91 92 int 93 rte_eal_timer_init(void) 94 { 95 set_tsc_freq(); 96 return 0; 97 } 98