]>
git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/ArmArchTimerLib/ArmArchTimerLib.c
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
11 #include <Library/ArmLib.h>
12 #include <Library/BaseLib.h>
13 #include <Library/TimerLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/PcdLib.h>
16 #include <Library/ArmGenericTimerCounterLib.h>
18 #define TICKS_PER_MICRO_SEC (PcdGet32 (PcdArmArchTimerFreqInHz)/1000000U)
20 // Select appropriate multiply function for platform architecture.
22 #define MULT_U64_X_N MultU64x32
24 #define MULT_U64_X_N MultU64x64
34 // Check if the ARM Generic Timer Extension is implemented.
36 if (ArmIsArchTimerImplemented ()) {
38 // Check if Architectural Timer frequency is pre-determined by the platform
41 if (PcdGet32 (PcdArmArchTimerFreqInHz
) != 0) {
43 // Check if ticks/uS is not 0. The Architectural timer runs at constant
44 // frequency, irrespective of CPU frequency. According to Generic Timer
45 // Ref manual, lower bound of the frequency is in the range of 1-10MHz.
47 ASSERT (TICKS_PER_MICRO_SEC
);
51 // Only set the frequency for ARMv7. We expect the secure firmware to
52 // have already done it.
53 // If the security extension is not implemented, set Timer Frequency
56 if (ArmHasSecurityExtensions ()) {
57 ArmGenericTimerSetTimerFreq (PcdGet32 (PcdArmArchTimerFreqInHz
));
64 // Architectural Timer Frequency must be set in Secure privileged
65 // mode (if secure extension is supported).
66 // If the reset value (0) is returned, just ASSERT.
68 ASSERT (ArmGenericTimerGetTimerFreq () != 0);
70 DEBUG ((DEBUG_ERROR
, "ARM Architectural Timer is not available in the CPU, hence this library cannot be used.\n"));
74 return RETURN_SUCCESS
;
78 A local utility function that returns the PCD value, if specified.
79 Otherwise it defaults to ArmGenericTimerGetTimerFreq.
81 @return The timer frequency.
87 GetPlatformTimerFreq (
92 TimerFreq
= PcdGet32 (PcdArmArchTimerFreqInHz
);
94 TimerFreq
= ArmGenericTimerGetTimerFreq ();
101 Stalls the CPU for the number of microseconds specified by MicroSeconds.
103 @param MicroSeconds The minimum number of microseconds to delay.
105 @return The value of MicroSeconds input.
111 IN UINTN MicroSeconds
115 UINT64 SystemCounterVal
;
117 // Calculate counter ticks that represent requested delay:
118 // = MicroSeconds x TICKS_PER_MICRO_SEC
119 // = MicroSeconds x Frequency.10^-6
120 TimerTicks64
= DivU64x32 (
123 GetPlatformTimerFreq ()
128 // Read System Counter value
129 SystemCounterVal
= ArmGenericTimerGetSystemCount ();
131 TimerTicks64
+= SystemCounterVal
;
133 // Wait until delay count expires.
134 while (SystemCounterVal
< TimerTicks64
) {
135 SystemCounterVal
= ArmGenericTimerGetSystemCount ();
142 Stalls the CPU for at least the given number of nanoseconds.
144 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
146 When the timer frequency is 1MHz, each tick corresponds to 1 microsecond.
147 Therefore, the nanosecond delay will be rounded up to the nearest 1 microsecond.
149 @param NanoSeconds The minimum number of nanoseconds to delay.
151 @return The value of NanoSeconds inputted.
162 // Round up to 1us Tick Number
163 MicroSeconds
= NanoSeconds
/ 1000;
164 MicroSeconds
+= ((NanoSeconds
% 1000) == 0) ? 0 : 1;
166 MicroSecondDelay (MicroSeconds
);
172 Retrieves the current value of a 64-bit free running performance counter.
174 The counter can either count up by 1 or count down by 1. If the physical
175 performance counter counts by a larger increment, then the counter values
176 must be translated. The properties of the counter can be retrieved from
177 GetPerformanceCounterProperties().
179 @return The current value of the free running performance counter.
184 GetPerformanceCounter (
188 // Just return the value of system count
189 return ArmGenericTimerGetSystemCount ();
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
) {
224 *StartValue
= (UINT64
)0ULL;
227 if (EndValue
!= NULL
) {
229 *EndValue
= 0xFFFFFFFFFFFFFFFFUL
;
232 return (UINT64
)ArmGenericTimerGetTimerFreq ();
236 Converts elapsed ticks of performance counter to time in nanoseconds.
238 This function converts the elapsed ticks of running performance counter to
239 time value in unit of nanoseconds.
241 @param Ticks The number of elapsed ticks of running performance counter.
243 @return The elapsed time in nanoseconds.
248 GetTimeInNanoSecond (
256 TimerFreq
= GetPlatformTimerFreq ();
259 // Time = --------- x 1,000,000,000
262 NanoSeconds
= MULT_U64_X_N (
272 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
273 // will not overflow 64-bit.
275 NanoSeconds
+= DivU64x32 (