2a35a10ccce9f6aa3a5b19d3373ce1b154660f2e
[reactos.git] / reactos / boot / environ / lib / platform / time.c
1 /*
2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/platform/time.c
5 * PURPOSE: Boot Library Time Management Routines
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "bl.h"
12
13 /* DATA VARIABLES ************************************************************/
14
15 ULONGLONG BlpTimePerformanceFrequency;
16
17 /* FUNCTIONS *****************************************************************/
18
19 NTSTATUS
20 BlpTimeMeasureTscFrequency (
21 VOID
22 )
23 {
24 ULONG Count;
25 INT CpuInfo[4];
26 ULONGLONG TimeStamp1, TimeStamp2, Delta;
27
28 /* Check if the ISVM bit it set, meaning we're in a hypervisor */
29 __cpuid(CpuInfo, 1);
30 Count = CpuInfo[2] & 0x80000000 ? 10 : 1;
31
32 /* Loop trying to get an accurate TSC */
33 do
34 {
35 /* Stall for 1us and get count 1 */
36 EfiStall(1);
37 TimeStamp1 = __rdtsc();
38
39 /* Stall for 1000us and get count 2*/
40 EfiStall(1000);
41 TimeStamp2 = __rdtsc();
42
43 /* Stall for 9000us and get the difference */
44 EfiStall(9000);
45 Delta = __rdtsc() - TimeStamp2;
46
47 /* Keep going as long as the TSC is fluctuating */
48 --Count;
49 } while (((TimeStamp2 - TimeStamp1) > Delta) && (Count));
50
51 /* Set the frequency based on the two measurements we took */
52 BlpTimePerformanceFrequency = 125 * (Delta - (TimeStamp2 - TimeStamp1)) & 0x1FFFFFFFFFFFFFF;
53 EarlyPrint(L"Computed frequency as: %I64d\n", BlpTimePerformanceFrequency);
54 return STATUS_SUCCESS;
55 }
56
57 NTSTATUS
58 BlpTimeCalibratePerformanceCounter (
59 VOID
60 )
61 {
62 INT CpuInfo[4];
63
64 /* Check if the ISVM bit it set, meaning we're in a hypervisor */
65 __cpuid(CpuInfo, 1);
66 if (CpuInfo[2] & 0x80000000)
67 {
68 /* Get the Hypervisor Identification Leaf */
69 __cpuid(CpuInfo, 0x40000001);
70
71 /* Is this Hyper-V? */
72 if (CpuInfo[0] == '1#vH')
73 {
74 /* Get the Hypervisor Feature Identification Leaf */
75 __cpuid(CpuInfo, 0x40000003);
76
77 /* Check if HV_X64_MSR_REFERENCE_TSC is present */
78 if (CpuInfo[3] & 0x100)
79 {
80 /* Read the TSC frequency from the MSR */
81 BlpTimePerformanceFrequency = __readmsr(0x40000022);
82 EarlyPrint(L"Using frequency as: %I64d\n", BlpTimePerformanceFrequency);
83 return STATUS_SUCCESS;
84 }
85 }
86 }
87
88 /* On other systems, compute it */
89 return BlpTimeMeasureTscFrequency();
90 }