]> git.proxmox.com Git - mirror_edk2.git/blob - EmulatorPkg/Library/DxeCoreTimerLib/DxeCoreTimerLib.c
EmulatorPkg/TimerLib: Add missing GetTimeInNanoSecond function
[mirror_edk2.git] / EmulatorPkg / Library / DxeCoreTimerLib / DxeCoreTimerLib.c
1 /** @file
2 A non-functional instance of the Timer Library.
3
4 Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiPei.h>
10 #include <Library/BaseLib.h>
11 #include <Library/TimerLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/EmuThunkLib.h>
14 #include <Library/UefiBootServicesTableLib.h>
15 #include <Library/UefiLib.h>
16
17 #include <Protocol/Timer.h>
18
19
20
21 /**
22 Stalls the CPU for at least the given number of microseconds.
23
24 Stalls the CPU for the number of microseconds specified by MicroSeconds.
25
26 @param MicroSeconds The minimum number of microseconds to delay.
27
28 @return The value of MicroSeconds inputted.
29
30 **/
31 UINTN
32 EFIAPI
33 MicroSecondDelay (
34 IN UINTN MicroSeconds
35 )
36 {
37 return NanoSecondDelay (MicroSeconds * 1000);
38 }
39
40
41 /**
42 Stalls the CPU for at least the given number of nanoseconds.
43
44 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
45
46 @param NanoSeconds The minimum number of nanoseconds to delay.
47
48 @return The value of NanoSeconds inputted.
49
50 **/
51 UINTN
52 EFIAPI
53 NanoSecondDelay (
54 IN UINTN NanoSeconds
55 )
56 {
57 gEmuThunk->Sleep (NanoSeconds);
58 return NanoSeconds;
59 }
60
61
62 /**
63 Retrieves the current value of a 64-bit free running performance counter.
64
65 The counter can either count up by 1 or count down by 1. If the physical
66 performance counter counts by a larger increment, then the counter values
67 must be translated. The properties of the counter can be retrieved from
68 GetPerformanceCounterProperties().
69
70 @return The current value of the free running performance counter.
71
72 **/
73 UINT64
74 EFIAPI
75 GetPerformanceCounter (
76 VOID
77 )
78 {
79 return gEmuThunk->QueryPerformanceCounter ();
80 }
81
82 /**
83 Retrieves the 64-bit frequency in Hz and the range of performance counter
84 values.
85
86 If StartValue is not NULL, then the value that the performance counter starts
87 with immediately after is it rolls over is returned in StartValue. If
88 EndValue is not NULL, then the value that the performance counter end with
89 immediately before it rolls over is returned in EndValue. The 64-bit
90 frequency of the performance counter in Hz is always returned. If StartValue
91 is less than EndValue, then the performance counter counts up. If StartValue
92 is greater than EndValue, then the performance counter counts down. For
93 example, a 64-bit free running counter that counts up would have a StartValue
94 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
95 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
96
97 @param StartValue The value the performance counter starts with when it
98 rolls over.
99 @param EndValue The value that the performance counter ends with before
100 it rolls over.
101
102 @return The frequency in Hz.
103
104 **/
105 UINT64
106 EFIAPI
107 GetPerformanceCounterProperties (
108 OUT UINT64 *StartValue, OPTIONAL
109 OUT UINT64 *EndValue OPTIONAL
110 )
111 {
112
113 if (StartValue != NULL) {
114 *StartValue = 0ULL;
115 }
116 if (EndValue != NULL) {
117 *EndValue = (UINT64)-1LL;
118 }
119
120 return gEmuThunk->QueryPerformanceFrequency ();
121 }
122
123 /**
124 Converts elapsed ticks of performance counter to time in nanoseconds.
125
126 This function converts the elapsed ticks of running performance counter to
127 time value in unit of nanoseconds.
128
129 @param Ticks The number of elapsed ticks of running performance counter.
130
131 @return The elapsed time in nanoseconds.
132
133 **/
134 UINT64
135 EFIAPI
136 GetTimeInNanoSecond (
137 IN UINT64 Ticks
138 )
139 {
140 UINT64 Frequency;
141 UINT64 NanoSeconds;
142 UINT64 Remainder;
143 INTN Shift;
144
145 Frequency = GetPerformanceCounterProperties (NULL, NULL);
146
147 //
148 // Ticks
149 // Time = --------- x 1,000,000,000
150 // Frequency
151 //
152 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);
153
154 //
155 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
156 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
157 // i.e. highest bit set in Remainder should <= 33.
158 //
159 Shift = MAX (0, HighBitSet64 (Remainder) - 33);
160 Remainder = RShiftU64 (Remainder, (UINTN) Shift);
161 Frequency = RShiftU64 (Frequency, (UINTN) Shift);
162 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);
163
164 return NanoSeconds;
165 }