]>
git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/ArmArchTimerLib/ArmArchTimerLib.c
4b1c9ac49efb8004668f705d62668cff1491d03f
2 Generic ARM implementation of TimerLib.h
4 Copyright (c) 2011 - 2021, Arm Limited. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/ArmLib.h>
13 #include <Library/BaseLib.h>
14 #include <Library/TimerLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/PcdLib.h>
17 #include <Library/ArmGenericTimerCounterLib.h>
19 #define TICKS_PER_MICRO_SEC (PcdGet32 (PcdArmArchTimerFreqInHz)/1000000U)
21 // Select appropriate multiply function for platform architecture.
23 #define MULT_U64_X_N MultU64x32
25 #define MULT_U64_X_N MultU64x64
36 // Check if the ARM Generic Timer Extension is implemented.
38 if (ArmIsArchTimerImplemented ()) {
41 // Check if Architectural Timer frequency is pre-determined by the platform
44 if (PcdGet32 (PcdArmArchTimerFreqInHz
) != 0) {
46 // Check if ticks/uS is not 0. The Architectural timer runs at constant
47 // frequency, irrespective of CPU frequency. According to Generic Timer
48 // Ref manual, lower bound of the frequency is in the range of 1-10MHz.
50 ASSERT (TICKS_PER_MICRO_SEC
);
54 // Only set the frequency for ARMv7. We expect the secure firmware to
55 // have already done it.
56 // If the security extension is not implemented, set Timer Frequency
59 if (ArmHasSecurityExtensions ()) {
60 ArmGenericTimerSetTimerFreq (PcdGet32 (PcdArmArchTimerFreqInHz
));
66 // Architectural Timer Frequency must be set in Secure privileged
67 // mode (if secure extension is supported).
68 // If the reset value (0) is returned, just ASSERT.
70 ASSERT (ArmGenericTimerGetTimerFreq () != 0);
73 DEBUG ((DEBUG_ERROR
, "ARM Architectural Timer is not available in the CPU, hence this library cannot be used.\n"));
77 return RETURN_SUCCESS
;
81 A local utility function that returns the PCD value, if specified.
82 Otherwise it defaults to ArmGenericTimerGetTimerFreq.
84 @return The timer frequency.
90 GetPlatformTimerFreq (
95 TimerFreq
= PcdGet32 (PcdArmArchTimerFreqInHz
);
97 TimerFreq
= ArmGenericTimerGetTimerFreq ();
104 Stalls the CPU for the number of microseconds specified by MicroSeconds.
106 @param MicroSeconds The minimum number of microseconds to delay.
108 @return The value of MicroSeconds input.
114 IN UINTN MicroSeconds
118 UINT64 SystemCounterVal
;
120 // Calculate counter ticks that represent requested delay:
121 // = MicroSeconds x TICKS_PER_MICRO_SEC
122 // = MicroSeconds x Frequency.10^-6
123 TimerTicks64
= DivU64x32 (
126 GetPlatformTimerFreq ()
131 // Read System Counter value
132 SystemCounterVal
= ArmGenericTimerGetSystemCount ();
134 TimerTicks64
+= SystemCounterVal
;
136 // Wait until delay count expires.
137 while (SystemCounterVal
< TimerTicks64
) {
138 SystemCounterVal
= ArmGenericTimerGetSystemCount ();
146 Stalls the CPU for at least the given number of nanoseconds.
148 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
150 When the timer frequency is 1MHz, each tick corresponds to 1 microsecond.
151 Therefore, the nanosecond delay will be rounded up to the nearest 1 microsecond.
153 @param NanoSeconds The minimum number of nanoseconds to delay.
155 @return The value of NanoSeconds inputted.
166 // Round up to 1us Tick Number
167 MicroSeconds
= NanoSeconds
/ 1000;
168 MicroSeconds
+= ((NanoSeconds
% 1000) == 0) ? 0 : 1;
170 MicroSecondDelay (MicroSeconds
);
176 Retrieves the current value of a 64-bit free running performance counter.
178 The counter can either count up by 1 or count down by 1. If the physical
179 performance counter counts by a larger increment, then the counter values
180 must be translated. The properties of the counter can be retrieved from
181 GetPerformanceCounterProperties().
183 @return The current value of the free running performance counter.
188 GetPerformanceCounter (
192 // Just return the value of system count
193 return ArmGenericTimerGetSystemCount ();
197 Retrieves the 64-bit frequency in Hz and the range of performance counter
200 If StartValue is not NULL, then the value that the performance counter starts
201 with immediately after is it rolls over is returned in StartValue. If
202 EndValue is not NULL, then the value that the performance counter end with
203 immediately before it rolls over is returned in EndValue. The 64-bit
204 frequency of the performance counter in Hz is always returned. If StartValue
205 is less than EndValue, then the performance counter counts up. If StartValue
206 is greater than EndValue, then the performance counter counts down. For
207 example, a 64-bit free running counter that counts up would have a StartValue
208 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
209 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
211 @param StartValue The value the performance counter starts with when it
213 @param EndValue The value that the performance counter ends with before
216 @return The frequency in Hz.
221 GetPerformanceCounterProperties (
222 OUT UINT64
*StartValue OPTIONAL
,
223 OUT UINT64
*EndValue OPTIONAL
226 if (StartValue
!= NULL
) {
228 *StartValue
= (UINT64
)0ULL ;
231 if (EndValue
!= NULL
) {
233 *EndValue
= 0xFFFFFFFFFFFFFFFFUL
;
236 return (UINT64
)ArmGenericTimerGetTimerFreq ();
240 Converts elapsed ticks of performance counter to time in nanoseconds.
242 This function converts the elapsed ticks of running performance counter to
243 time value in unit of nanoseconds.
245 @param Ticks The number of elapsed ticks of running performance counter.
247 @return The elapsed time in nanoseconds.
252 GetTimeInNanoSecond (
260 TimerFreq
= GetPlatformTimerFreq ();
263 // Time = --------- x 1,000,000,000
266 NanoSeconds
= MULT_U64_X_N (
275 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
276 // will not overflow 64-bit.
278 NanoSeconds
+= DivU64x32 (