]> git.proxmox.com Git - mirror_edk2.git/blame - CorebootPayloadPkg/Library/AcpiTimerLib/AcpiTimerLib.c
Pkg-Module: CorebootPayloadPkg
[mirror_edk2.git] / CorebootPayloadPkg / Library / AcpiTimerLib / AcpiTimerLib.c
CommitLineData
9c228fb0
MM
1/** @file\r
2 ACPI Timer implements one instance of Timer Library.\r
3\r
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials are\r
6 licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <PiPei.h>\r
16#include <Library/TimerLib.h>\r
17#include <Library/BaseLib.h>\r
18#include <Library/IoLib.h>\r
19#include <Library/HobLib.h>\r
20#include <Library/DebugLib.h>\r
21\r
22#include <Guid/AcpiBoardInfoGuid.h>\r
23#include <IndustryStandard/Acpi.h>\r
24\r
25#define ACPI_TIMER_COUNT_SIZE BIT24\r
26\r
27UINTN mPmTimerReg = 0;\r
28\r
29/**\r
30 The constructor function enables ACPI IO space.\r
31\r
32 If ACPI I/O space not enabled, this function will enable it.\r
33 It will always return RETURN_SUCCESS.\r
34\r
35 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.\r
36\r
37**/\r
38RETURN_STATUS\r
39EFIAPI\r
40AcpiTimerLibConstructor (\r
41 VOID\r
42 )\r
43{\r
44 EFI_HOB_GUID_TYPE *GuidHob;\r
45 ACPI_BOARD_INFO *pAcpiBoardInfo; \r
46 \r
47 //\r
48 // Find the acpi board information guid hob\r
49 //\r
50 GuidHob = GetFirstGuidHob (&gUefiAcpiBoardInfoGuid);\r
51 ASSERT (GuidHob != NULL);\r
52 \r
53 pAcpiBoardInfo = (ACPI_BOARD_INFO *)GET_GUID_HOB_DATA (GuidHob); \r
54 \r
55 mPmTimerReg = (UINTN)pAcpiBoardInfo->PmTimerRegBase;\r
56 \r
57 return EFI_SUCCESS;\r
58}\r
59\r
60/**\r
61 Internal function to read the current tick counter of ACPI.\r
62\r
63 Internal function to read the current tick counter of ACPI.\r
64\r
65 @return The tick counter read.\r
66\r
67**/\r
68UINT32\r
69InternalAcpiGetTimerTick (\r
70 VOID\r
71 )\r
72{\r
73 if (mPmTimerReg == 0)\r
74 AcpiTimerLibConstructor ();\r
75 \r
76 return IoRead32 (mPmTimerReg);\r
77}\r
78\r
79/**\r
80 Stalls the CPU for at least the given number of ticks.\r
81\r
82 Stalls the CPU for at least the given number of ticks. It's invoked by\r
83 MicroSecondDelay() and NanoSecondDelay().\r
84\r
85 @param Delay A period of time to delay in ticks.\r
86\r
87**/\r
88VOID\r
89InternalAcpiDelay (\r
90 IN UINT32 Delay\r
91 )\r
92{\r
93 UINT32 Ticks;\r
94 UINT32 Times;\r
95\r
96 Times = Delay >> 22;\r
97 Delay &= BIT22 - 1;\r
98 do {\r
99 //\r
100 // The target timer count is calculated here\r
101 //\r
102 Ticks = InternalAcpiGetTimerTick () + Delay;\r
103 Delay = BIT22;\r
104 //\r
105 // Wait until time out\r
106 // Delay >= 2^23 could not be handled by this function\r
107 // Timer wrap-arounds are handled correctly by this function\r
108 //\r
109 while (((Ticks - InternalAcpiGetTimerTick ()) & BIT23) == 0) {\r
110 CpuPause ();\r
111 }\r
112 } while (Times-- > 0);\r
113}\r
114\r
115/**\r
116 Stalls the CPU for at least the given number of microseconds.\r
117\r
118 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
119\r
120 @param MicroSeconds The minimum number of microseconds to delay.\r
121\r
122 @return MicroSeconds\r
123\r
124**/\r
125UINTN\r
126EFIAPI\r
127MicroSecondDelay (\r
128 IN UINTN MicroSeconds\r
129 )\r
130{\r
131 InternalAcpiDelay (\r
132 (UINT32)DivU64x32 (\r
133 MultU64x32 (\r
134 MicroSeconds,\r
135 ACPI_TIMER_FREQUENCY\r
136 ),\r
137 1000000u\r
138 )\r
139 );\r
140 return MicroSeconds;\r
141}\r
142\r
143/**\r
144 Stalls the CPU for at least the given number of nanoseconds.\r
145\r
146 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
147\r
148 @param NanoSeconds The minimum number of nanoseconds to delay.\r
149\r
150 @return NanoSeconds\r
151\r
152**/\r
153UINTN\r
154EFIAPI\r
155NanoSecondDelay (\r
156 IN UINTN NanoSeconds\r
157 )\r
158{\r
159 InternalAcpiDelay (\r
160 (UINT32)DivU64x32 (\r
161 MultU64x32 (\r
162 NanoSeconds,\r
163 ACPI_TIMER_FREQUENCY\r
164 ),\r
165 1000000000u\r
166 )\r
167 );\r
168 return NanoSeconds;\r
169}\r
170\r
171/**\r
172 Retrieves the current value of a 64-bit free running performance counter.\r
173\r
174 Retrieves the current value of a 64-bit free running performance counter. The\r
175 counter can either count up by 1 or count down by 1. If the physical\r
176 performance counter counts by a larger increment, then the counter values\r
177 must be translated. The properties of the counter can be retrieved from\r
178 GetPerformanceCounterProperties().\r
179\r
180 @return The current value of the free running performance counter.\r
181\r
182**/\r
183UINT64\r
184EFIAPI\r
185GetPerformanceCounter (\r
186 VOID\r
187 )\r
188{\r
189 return (UINT64)InternalAcpiGetTimerTick ();\r
190}\r
191\r
192/**\r
193 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
194 values.\r
195\r
196 If StartValue is not NULL, then the value that the performance counter starts\r
197 with immediately after is it rolls over is returned in StartValue. If\r
198 EndValue is not NULL, then the value that the performance counter end with\r
199 immediately before it rolls over is returned in EndValue. The 64-bit\r
200 frequency of the performance counter in Hz is always returned. If StartValue\r
201 is less than EndValue, then the performance counter counts up. If StartValue\r
202 is greater than EndValue, then the performance counter counts down. For\r
203 example, a 64-bit free running counter that counts up would have a StartValue\r
204 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
205 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
206\r
207 @param StartValue The value the performance counter starts with when it\r
208 rolls over.\r
209 @param EndValue The value that the performance counter ends with before\r
210 it rolls over.\r
211\r
212 @return The frequency in Hz.\r
213\r
214**/\r
215UINT64\r
216EFIAPI\r
217GetPerformanceCounterProperties (\r
218 OUT UINT64 *StartValue, OPTIONAL\r
219 OUT UINT64 *EndValue OPTIONAL\r
220 )\r
221{\r
222 if (StartValue != NULL) {\r
223 *StartValue = 0;\r
224 }\r
225\r
226 if (EndValue != NULL) {\r
227 *EndValue = ACPI_TIMER_COUNT_SIZE - 1;\r
228 }\r
229\r
230 return ACPI_TIMER_FREQUENCY;\r
231}\r
232\r
233/**\r
234 Converts elapsed ticks of performance counter to time in nanoseconds.\r
235\r
236 This function converts the elapsed ticks of running performance counter to\r
237 time value in unit of nanoseconds.\r
238\r
239 @param Ticks The number of elapsed ticks of running performance counter.\r
240\r
241 @return The elapsed time in nanoseconds.\r
242\r
243**/\r
244UINT64\r
245EFIAPI\r
246GetTimeInNanoSecond (\r
247 IN UINT64 Ticks\r
248 )\r
249{\r
250 UINT64 Frequency;\r
251 UINT64 NanoSeconds;\r
252 UINT64 Remainder;\r
253 INTN Shift;\r
254\r
255 Frequency = GetPerformanceCounterProperties (NULL, NULL);\r
256\r
257 //\r
258 // Ticks\r
259 // Time = --------- x 1,000,000,000\r
260 // Frequency\r
261 //\r
262 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);\r
263\r
264 //\r
265 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.\r
266 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,\r
267 // i.e. highest bit set in Remainder should <= 33.\r
268 //\r
269 Shift = MAX (0, HighBitSet64 (Remainder) - 33);\r
270 Remainder = RShiftU64 (Remainder, (UINTN) Shift);\r
271 Frequency = RShiftU64 (Frequency, (UINTN) Shift);\r
272 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);\r
273\r
274 return NanoSeconds;\r
275}\r
276\r