]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/AcpiTimerLib/AcpiTimerLib.c
When SOURCE_DEBUG_ENABLE is set, a TimerLib is linked into the SEC Phase to support...
[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
17#include <Base.h>\r
18#include <Library/TimerLib.h>\r
19#include <Library/BaseLib.h>\r
20#include <Library/IoLib.h>\r
21#include <Library/PciLib.h>\r
550486ed 22#include <Library/DebugLib.h>\r
830067bf 23#include <Library/PcdLib.h>\r
5a624588 24#include <IndustryStandard/Pci22.h>\r
d3a24ff5 25#include <IndustryStandard/Acpi.h>\r
550486ed 26\r
27//\r
d3a24ff5 28// PCI Location of PIIX4 Power Management PCI Configuration Registers\r
550486ed 29//\r
d3a24ff5 30#define PIIX4_POWER_MANAGEMENT_BUS 0x00\r
31#define PIIX4_POWER_MANAGEMENT_DEVICE 0x01\r
32#define PIIX4_POWER_MANAGEMENT_FUNCTION 0x03\r
49ba9447 33\r
d3a24ff5 34//\r
35// Macro to access PIIX4 Power Management PCI Configuration Registers\r
36//\r
37#define PIIX4_PCI_POWER_MANAGEMENT_REGISTER(Register) \\r
38 PCI_LIB_ADDRESS ( \\r
39 PIIX4_POWER_MANAGEMENT_BUS, \\r
40 PIIX4_POWER_MANAGEMENT_DEVICE, \\r
41 PIIX4_POWER_MANAGEMENT_FUNCTION, \\r
42 Register \\r
43 )\r
44\r
45//\r
46// PIIX4 Power Management PCI Configuration Registers\r
47//\r
48#define PMBA PIIX4_PCI_POWER_MANAGEMENT_REGISTER (0x40)\r
49#define PMBA_RTE BIT0\r
50#define PMREGMISC PIIX4_PCI_POWER_MANAGEMENT_REGISTER (0x80)\r
51#define PMIOSE BIT0\r
52\r
53//\r
54// The ACPI Time in the PIIX4 is a 24-bit counter\r
55//\r
56#define ACPI_TIMER_COUNT_SIZE BIT24\r
57\r
58//\r
59// Offset in the PIIX4 Power Management Base Address to the ACPI Timer \r
60//\r
550486ed 61#define ACPI_TIMER_OFFSET 0x8\r
49ba9447 62\r
63/**\r
64 The constructor function enables ACPI IO space.\r
65\r
66 If ACPI I/O space not enabled, this function will enable it.\r
67 It will always return RETURN_SUCCESS.\r
68\r
69 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.\r
70\r
71**/\r
72RETURN_STATUS\r
73EFIAPI\r
74AcpiTimerLibConstructor (\r
75 VOID\r
76 )\r
77{\r
49ba9447 78 //\r
d3a24ff5 79 // Check to see if the PIIX4 Power Management Base Address is already enabled\r
49ba9447 80 //\r
d3a24ff5 81 if ((PciRead8 (PMREGMISC) & PMIOSE) == 0) {\r
82 //\r
83 // If the PIIX4 Power Management Base Address is not programmed, \r
84 // then program the PIIX4 Power Management Base Address from a PCD.\r
85 //\r
86 PciAndThenOr32 (PMBA, (UINT32)(~0x0000FFC0), PcdGet16 (PcdAcpiPmBaseAddress));\r
87\r
88 //\r
89 // Enable PMBA I/O port decodes in PMREGMISC\r
90 //\r
91 PciOr8 (PMREGMISC, PMIOSE);\r
92 }\r
93 \r
550486ed 94 return RETURN_SUCCESS;\r
49ba9447 95}\r
96\r
97/**\r
98 Internal function to read the current tick counter of ACPI.\r
99\r
100 Internal function to read the current tick counter of ACPI.\r
101\r
102 @return The tick counter read.\r
103\r
104**/\r
49ba9447 105UINT32\r
106InternalAcpiGetTimerTick (\r
107 VOID\r
108 )\r
109{\r
d3a24ff5 110 //\r
111 // Read PMBA to read and return the current ACPI timer value.\r
112 //\r
113 return IoRead32 ((PciRead32 (PMBA) & ~PMBA_RTE) + ACPI_TIMER_OFFSET);\r
49ba9447 114}\r
115\r
116/**\r
117 Stalls the CPU for at least the given number of ticks.\r
118\r
119 Stalls the CPU for at least the given number of ticks. It's invoked by\r
120 MicroSecondDelay() and NanoSecondDelay().\r
121\r
122 @param Delay A period of time to delay in ticks.\r
123\r
124**/\r
49ba9447 125VOID\r
126InternalAcpiDelay (\r
127 IN UINT32 Delay\r
128 )\r
129{\r
130 UINT32 Ticks;\r
131 UINT32 Times;\r
132\r
133 Times = Delay >> 22;\r
134 Delay &= BIT22 - 1;\r
135 do {\r
136 //\r
137 // The target timer count is calculated here\r
138 //\r
139 Ticks = InternalAcpiGetTimerTick () + Delay;\r
140 Delay = BIT22;\r
141 //\r
142 // Wait until time out\r
143 // Delay >= 2^23 could not be handled by this function\r
144 // Timer wrap-arounds are handled correctly by this function\r
145 //\r
146 while (((Ticks - InternalAcpiGetTimerTick ()) & BIT23) == 0) {\r
147 CpuPause ();\r
148 }\r
149 } while (Times-- > 0);\r
150}\r
151\r
152/**\r
153 Stalls the CPU for at least the given number of microseconds.\r
154\r
155 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
156\r
157 @param MicroSeconds The minimum number of microseconds to delay.\r
158\r
159 @return MicroSeconds\r
160\r
161**/\r
162UINTN\r
163EFIAPI\r
164MicroSecondDelay (\r
165 IN UINTN MicroSeconds\r
166 )\r
167{\r
168 InternalAcpiDelay (\r
169 (UINT32)DivU64x32 (\r
170 MultU64x32 (\r
171 MicroSeconds,\r
172 ACPI_TIMER_FREQUENCY\r
173 ),\r
174 1000000u\r
175 )\r
176 );\r
177 return MicroSeconds;\r
178}\r
179\r
180/**\r
181 Stalls the CPU for at least the given number of nanoseconds.\r
182\r
183 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
184\r
185 @param NanoSeconds The minimum number of nanoseconds to delay.\r
186\r
187 @return NanoSeconds\r
188\r
189**/\r
190UINTN\r
191EFIAPI\r
192NanoSecondDelay (\r
193 IN UINTN NanoSeconds\r
194 )\r
195{\r
196 InternalAcpiDelay (\r
197 (UINT32)DivU64x32 (\r
198 MultU64x32 (\r
199 NanoSeconds,\r
200 ACPI_TIMER_FREQUENCY\r
201 ),\r
202 1000000000u\r
203 )\r
204 );\r
205 return NanoSeconds;\r
206}\r
207\r
208/**\r
209 Retrieves the current value of a 64-bit free running performance counter.\r
210\r
211 Retrieves the current value of a 64-bit free running performance counter. The\r
212 counter can either count up by 1 or count down by 1. If the physical\r
213 performance counter counts by a larger increment, then the counter values\r
214 must be translated. The properties of the counter can be retrieved from\r
215 GetPerformanceCounterProperties().\r
216\r
217 @return The current value of the free running performance counter.\r
218\r
219**/\r
220UINT64\r
221EFIAPI\r
222GetPerformanceCounter (\r
223 VOID\r
224 )\r
225{\r
226 return (UINT64)InternalAcpiGetTimerTick ();\r
227}\r
228\r
229/**\r
230 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
231 values.\r
232\r
233 If StartValue is not NULL, then the value that the performance counter starts\r
234 with immediately after is it rolls over is returned in StartValue. If\r
235 EndValue is not NULL, then the value that the performance counter end with\r
236 immediately before it rolls over is returned in EndValue. The 64-bit\r
237 frequency of the performance counter in Hz is always returned. If StartValue\r
238 is less than EndValue, then the performance counter counts up. If StartValue\r
239 is greater than EndValue, then the performance counter counts down. For\r
240 example, a 64-bit free running counter that counts up would have a StartValue\r
241 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
242 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
243\r
244 @param StartValue The value the performance counter starts with when it\r
245 rolls over.\r
246 @param EndValue The value that the performance counter ends with before\r
247 it rolls over.\r
248\r
249 @return The frequency in Hz.\r
250\r
251**/\r
252UINT64\r
253EFIAPI\r
254GetPerformanceCounterProperties (\r
255 OUT UINT64 *StartValue, OPTIONAL\r
256 OUT UINT64 *EndValue OPTIONAL\r
257 )\r
258{\r
259 if (StartValue != NULL) {\r
260 *StartValue = 0;\r
261 }\r
262\r
263 if (EndValue != NULL) {\r
264 *EndValue = ACPI_TIMER_COUNT_SIZE - 1;\r
265 }\r
266\r
267 return ACPI_TIMER_FREQUENCY;\r
268}\r
b9610b9c 269\r
270/**\r
271 Converts elapsed ticks of performance counter to time in nanoseconds.\r
272\r
273 This function converts the elapsed ticks of running performance counter to\r
274 time value in unit of nanoseconds.\r
275\r
276 @param Ticks The number of elapsed ticks of running performance counter.\r
277\r
278 @return The elapsed time in nanoseconds.\r
279\r
280**/\r
281UINT64\r
282EFIAPI\r
283GetTimeInNanoSecond (\r
284 IN UINT64 Ticks\r
285 )\r
286{\r
287 UINT64 NanoSeconds;\r
288 UINT32 Remainder;\r
289\r
290 //\r
291 // Ticks\r
292 // Time = --------- x 1,000,000,000\r
293 // Frequency\r
294 //\r
295 NanoSeconds = MultU64x32 (DivU64x32Remainder (Ticks, ACPI_TIMER_FREQUENCY, &Remainder), 1000000000u);\r
296\r
297 //\r
298 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)\r
299 // will not overflow 64-bit.\r
300 //\r
301 NanoSeconds += DivU64x32 (MultU64x32 ((UINT64) Remainder, 1000000000u), ACPI_TIMER_FREQUENCY);\r
302\r
303 return NanoSeconds;\r
304}\r