]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.c
OvmfPkg: QemuFlashFvbServicesRuntimeDxe: remove FvbDevLock field
[mirror_edk2.git] / OvmfPkg / Library / AcpiTimerLib / AcpiTimerLib.c
CommitLineData
49ba9447 1/** @file\r
2 ACPI Timer implements one instance of Timer Library.\r
3\r
d3a24ff5 4 Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>\r
550486ed 5 Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>\r
6\r
56d7640a 7 This program and the accompanying materials are\r
49ba9447 8 licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
67164dcd 11\r
49ba9447 12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
67164dcd 15**/\r
49ba9447 16\r
550486ed 17#include <Library/DebugLib.h>\r
170ef2d9 18#include <Library/BaseLib.h>\r
d3a24ff5 19#include <IndustryStandard/Acpi.h>\r
550486ed 20\r
170ef2d9 21#include "AcpiTimerLib.h"\r
d3a24ff5 22\r
23//\r
41f80fbd 24// The ACPI Time is a 24-bit counter\r
d3a24ff5 25//\r
26#define ACPI_TIMER_COUNT_SIZE BIT24\r
27\r
49ba9447 28/**\r
29 Stalls the CPU for at least the given number of ticks.\r
30\r
31 Stalls the CPU for at least the given number of ticks. It's invoked by\r
32 MicroSecondDelay() and NanoSecondDelay().\r
33\r
34 @param Delay A period of time to delay in ticks.\r
35\r
36**/\r
49ba9447 37VOID\r
38InternalAcpiDelay (\r
39 IN UINT32 Delay\r
40 )\r
41{\r
42 UINT32 Ticks;\r
43 UINT32 Times;\r
44\r
45 Times = Delay >> 22;\r
46 Delay &= BIT22 - 1;\r
47 do {\r
48 //\r
49 // The target timer count is calculated here\r
50 //\r
51 Ticks = InternalAcpiGetTimerTick () + Delay;\r
52 Delay = BIT22;\r
53 //\r
54 // Wait until time out\r
55 // Delay >= 2^23 could not be handled by this function\r
56 // Timer wrap-arounds are handled correctly by this function\r
57 //\r
58 while (((Ticks - InternalAcpiGetTimerTick ()) & BIT23) == 0) {\r
59 CpuPause ();\r
60 }\r
61 } while (Times-- > 0);\r
62}\r
63\r
64/**\r
65 Stalls the CPU for at least the given number of microseconds.\r
66\r
67 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
68\r
69 @param MicroSeconds The minimum number of microseconds to delay.\r
70\r
71 @return MicroSeconds\r
72\r
73**/\r
74UINTN\r
75EFIAPI\r
76MicroSecondDelay (\r
77 IN UINTN MicroSeconds\r
78 )\r
79{\r
80 InternalAcpiDelay (\r
81 (UINT32)DivU64x32 (\r
82 MultU64x32 (\r
83 MicroSeconds,\r
84 ACPI_TIMER_FREQUENCY\r
85 ),\r
86 1000000u\r
87 )\r
88 );\r
89 return MicroSeconds;\r
90}\r
91\r
92/**\r
93 Stalls the CPU for at least the given number of nanoseconds.\r
94\r
95 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
96\r
97 @param NanoSeconds The minimum number of nanoseconds to delay.\r
98\r
99 @return NanoSeconds\r
100\r
101**/\r
102UINTN\r
103EFIAPI\r
104NanoSecondDelay (\r
105 IN UINTN NanoSeconds\r
106 )\r
107{\r
108 InternalAcpiDelay (\r
109 (UINT32)DivU64x32 (\r
110 MultU64x32 (\r
111 NanoSeconds,\r
112 ACPI_TIMER_FREQUENCY\r
113 ),\r
114 1000000000u\r
115 )\r
116 );\r
117 return NanoSeconds;\r
118}\r
119\r
120/**\r
121 Retrieves the current value of a 64-bit free running performance counter.\r
122\r
123 Retrieves the current value of a 64-bit free running performance counter. The\r
124 counter can either count up by 1 or count down by 1. If the physical\r
125 performance counter counts by a larger increment, then the counter values\r
126 must be translated. The properties of the counter can be retrieved from\r
127 GetPerformanceCounterProperties().\r
128\r
129 @return The current value of the free running performance counter.\r
130\r
131**/\r
132UINT64\r
133EFIAPI\r
134GetPerformanceCounter (\r
135 VOID\r
136 )\r
137{\r
138 return (UINT64)InternalAcpiGetTimerTick ();\r
139}\r
140\r
141/**\r
142 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
143 values.\r
144\r
145 If StartValue is not NULL, then the value that the performance counter starts\r
146 with immediately after is it rolls over is returned in StartValue. If\r
147 EndValue is not NULL, then the value that the performance counter end with\r
148 immediately before it rolls over is returned in EndValue. The 64-bit\r
149 frequency of the performance counter in Hz is always returned. If StartValue\r
150 is less than EndValue, then the performance counter counts up. If StartValue\r
151 is greater than EndValue, then the performance counter counts down. For\r
152 example, a 64-bit free running counter that counts up would have a StartValue\r
153 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
154 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
155\r
156 @param StartValue The value the performance counter starts with when it\r
157 rolls over.\r
158 @param EndValue The value that the performance counter ends with before\r
159 it rolls over.\r
160\r
161 @return The frequency in Hz.\r
162\r
163**/\r
164UINT64\r
165EFIAPI\r
166GetPerformanceCounterProperties (\r
167 OUT UINT64 *StartValue, OPTIONAL\r
168 OUT UINT64 *EndValue OPTIONAL\r
169 )\r
170{\r
171 if (StartValue != NULL) {\r
172 *StartValue = 0;\r
173 }\r
174\r
175 if (EndValue != NULL) {\r
176 *EndValue = ACPI_TIMER_COUNT_SIZE - 1;\r
177 }\r
178\r
179 return ACPI_TIMER_FREQUENCY;\r
180}\r
b9610b9c 181\r
182/**\r
183 Converts elapsed ticks of performance counter to time in nanoseconds.\r
184\r
185 This function converts the elapsed ticks of running performance counter to\r
186 time value in unit of nanoseconds.\r
187\r
188 @param Ticks The number of elapsed ticks of running performance counter.\r
189\r
190 @return The elapsed time in nanoseconds.\r
191\r
192**/\r
193UINT64\r
194EFIAPI\r
195GetTimeInNanoSecond (\r
196 IN UINT64 Ticks\r
197 )\r
198{\r
199 UINT64 NanoSeconds;\r
200 UINT32 Remainder;\r
201\r
202 //\r
203 // Ticks\r
204 // Time = --------- x 1,000,000,000\r
205 // Frequency\r
206 //\r
207 NanoSeconds = MultU64x32 (DivU64x32Remainder (Ticks, ACPI_TIMER_FREQUENCY, &Remainder), 1000000000u);\r
208\r
209 //\r
210 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)\r
211 // will not overflow 64-bit.\r
212 //\r
213 NanoSeconds += DivU64x32 (MultU64x32 ((UINT64) Remainder, 1000000000u), ACPI_TIMER_FREQUENCY);\r
214\r
215 return NanoSeconds;\r
216}\r