]>
git.proxmox.com Git - mirror_edk2.git/blob - CorebootPayloadPkg/Library/AcpiTimerLib/AcpiTimerLib.c
2 ACPI Timer implements one instance of Timer Library.
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are
6 licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/TimerLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/IoLib.h>
19 #include <Library/HobLib.h>
20 #include <Library/DebugLib.h>
22 #include <Guid/AcpiBoardInfoGuid.h>
23 #include <IndustryStandard/Acpi.h>
25 #define ACPI_TIMER_COUNT_SIZE BIT24
27 UINTN mPmTimerReg
= 0;
30 The constructor function enables ACPI IO space.
32 If ACPI I/O space not enabled, this function will enable it.
33 It will always return RETURN_SUCCESS.
35 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
40 AcpiTimerLibConstructor (
44 EFI_HOB_GUID_TYPE
*GuidHob
;
45 ACPI_BOARD_INFO
*pAcpiBoardInfo
;
48 // Find the acpi board information guid hob
50 GuidHob
= GetFirstGuidHob (&gUefiAcpiBoardInfoGuid
);
51 ASSERT (GuidHob
!= NULL
);
53 pAcpiBoardInfo
= (ACPI_BOARD_INFO
*)GET_GUID_HOB_DATA (GuidHob
);
55 mPmTimerReg
= (UINTN
)pAcpiBoardInfo
->PmTimerRegBase
;
61 Internal function to read the current tick counter of ACPI.
63 Internal function to read the current tick counter of ACPI.
65 @return The tick counter read.
69 InternalAcpiGetTimerTick (
74 AcpiTimerLibConstructor ();
76 return IoRead32 (mPmTimerReg
);
80 Stalls the CPU for at least the given number of ticks.
82 Stalls the CPU for at least the given number of ticks. It's invoked by
83 MicroSecondDelay() and NanoSecondDelay().
85 @param Delay A period of time to delay in ticks.
100 // The target timer count is calculated here
102 Ticks
= InternalAcpiGetTimerTick () + Delay
;
105 // Wait until time out
106 // Delay >= 2^23 could not be handled by this function
107 // Timer wrap-arounds are handled correctly by this function
109 while (((Ticks
- InternalAcpiGetTimerTick ()) & BIT23
) == 0) {
112 } while (Times
-- > 0);
116 Stalls the CPU for at least the given number of microseconds.
118 Stalls the CPU for the number of microseconds specified by MicroSeconds.
120 @param MicroSeconds The minimum number of microseconds to delay.
128 IN UINTN MicroSeconds
144 Stalls the CPU for at least the given number of nanoseconds.
146 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
148 @param NanoSeconds The minimum number of nanoseconds to delay.
172 Retrieves the current value of a 64-bit free running performance counter.
174 Retrieves the current value of a 64-bit free running performance counter. The
175 counter can either count up by 1 or count down by 1. If the physical
176 performance counter counts by a larger increment, then the counter values
177 must be translated. The properties of the counter can be retrieved from
178 GetPerformanceCounterProperties().
180 @return The current value of the free running performance counter.
185 GetPerformanceCounter (
189 return (UINT64
)InternalAcpiGetTimerTick ();
193 Retrieves the 64-bit frequency in Hz and the range of performance counter
196 If StartValue is not NULL, then the value that the performance counter starts
197 with immediately after is it rolls over is returned in StartValue. If
198 EndValue is not NULL, then the value that the performance counter end with
199 immediately before it rolls over is returned in EndValue. The 64-bit
200 frequency of the performance counter in Hz is always returned. If StartValue
201 is less than EndValue, then the performance counter counts up. If StartValue
202 is greater than EndValue, then the performance counter counts down. For
203 example, a 64-bit free running counter that counts up would have a StartValue
204 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
205 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
207 @param StartValue The value the performance counter starts with when it
209 @param EndValue The value that the performance counter ends with before
212 @return The frequency in Hz.
217 GetPerformanceCounterProperties (
218 OUT UINT64
*StartValue
, OPTIONAL
219 OUT UINT64
*EndValue OPTIONAL
222 if (StartValue
!= NULL
) {
226 if (EndValue
!= NULL
) {
227 *EndValue
= ACPI_TIMER_COUNT_SIZE
- 1;
230 return ACPI_TIMER_FREQUENCY
;
234 Converts elapsed ticks of performance counter to time in nanoseconds.
236 This function converts the elapsed ticks of running performance counter to
237 time value in unit of nanoseconds.
239 @param Ticks The number of elapsed ticks of running performance counter.
241 @return The elapsed time in nanoseconds.
246 GetTimeInNanoSecond (
255 Frequency
= GetPerformanceCounterProperties (NULL
, NULL
);
259 // Time = --------- x 1,000,000,000
262 NanoSeconds
= MultU64x32 (DivU64x64Remainder (Ticks
, Frequency
, &Remainder
), 1000000000u);
265 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
266 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
267 // i.e. highest bit set in Remainder should <= 33.
269 Shift
= MAX (0, HighBitSet64 (Remainder
) - 33);
270 Remainder
= RShiftU64 (Remainder
, (UINTN
) Shift
);
271 Frequency
= RShiftU64 (Frequency
, (UINTN
) Shift
);
272 NanoSeconds
+= DivU64x64Remainder (MultU64x32 (Remainder
, 1000000000u), Frequency
, NULL
);