]>
git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/ArmArchTimerLib/ArmArchTimerLib.c
c0ab4a4433e431e806211495a532b07d22cb4e81
2 Generic ARM implementation of TimerLib.h
4 Copyright (c) 2011-2014, ARM Limited. All rights reserved.
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/ArmLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/TimerLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/PcdLib.h>
23 #include <Library/ArmGenericTimerCounterLib.h>
25 #define TICKS_PER_MICRO_SEC (PcdGet32 (PcdArmArchTimerFreqInHz)/1000000U)
33 // Check if the ARM Generic Timer Extension is implemented
34 if (ArmIsArchTimerImplemented ()) {
38 // Check if Architectural Timer frequency is valid number (should not be 0)
39 ASSERT (PcdGet32 (PcdArmArchTimerFreqInHz
));
41 // Check if ticks/uS is not 0. The Architectural timer runs at constant
42 // frequency irrespective of CPU frequency. According to General Timer Ref
43 // manual lower bound of the frequency is in the range of 1-10MHz
44 ASSERT (TICKS_PER_MICRO_SEC
);
47 // Only set the frequency for ARMv7. We expect the secure firmware to have already do it
48 // If the security extensions are not implemented set Timer Frequency
49 if ((ArmReadIdPfr1 () & ARM_PFR1_SEC
) == 0x0) {
50 ArmGenericTimerSetTimerFreq (PcdGet32 (PcdArmArchTimerFreqInHz
));
54 // Architectural Timer Frequency must be set in the Secure privileged(if secure extensions are supported) mode.
55 // If the reset value (0) is returned just ASSERT.
56 TimerFreq
= ArmGenericTimerGetTimerFreq ();
57 ASSERT (TimerFreq
!= 0);
60 DEBUG ((EFI_D_ERROR
, "ARM Architectural Timer is not available in the CPU, hence this library can not be used.\n"));
64 return RETURN_SUCCESS
;
69 Stalls the CPU for the number of microseconds specified by MicroSeconds.
71 @param MicroSeconds The minimum number of microseconds to delay.
73 @return The value of MicroSeconds inputted.
83 UINT64 SystemCounterVal
;
85 // Calculate counter ticks that can represent requested delay:
86 // = MicroSeconds x TICKS_PER_MICRO_SEC
87 // = MicroSeconds x Frequency.10^-6
88 TimerTicks64
= ((UINT64
)MicroSeconds
* PcdGet32 (PcdArmArchTimerFreqInHz
)) / 1000000U;
90 // Read System Counter value
91 SystemCounterVal
= ArmGenericTimerGetSystemCount ();
93 TimerTicks64
+= SystemCounterVal
;
95 // Wait until delay count is expired.
96 while (SystemCounterVal
< TimerTicks64
) {
97 SystemCounterVal
= ArmGenericTimerGetSystemCount ();
105 Stalls the CPU for at least the given number of nanoseconds.
107 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
109 When the timer frequency is 1MHz, each tick corresponds to 1 microsecond.
110 Therefore, the nanosecond delay will be rounded up to the nearest 1 microsecond.
112 @param NanoSeconds The minimum number of nanoseconds to delay.
114 @return The value of NanoSeconds inputed.
125 // Round up to 1us Tick Number
126 MicroSeconds
= NanoSeconds
/ 1000;
127 MicroSeconds
+= ((NanoSeconds
% 1000) == 0) ? 0 : 1;
129 MicroSecondDelay (MicroSeconds
);
135 Retrieves the current value of a 64-bit free running performance counter.
137 The counter can either count up by 1 or count down by 1. If the physical
138 performance counter counts by a larger increment, then the counter values
139 must be translated. The properties of the counter can be retrieved from
140 GetPerformanceCounterProperties().
142 @return The current value of the free running performance counter.
147 GetPerformanceCounter (
151 // Just return the value of system count
152 return ArmGenericTimerGetSystemCount ();
156 Retrieves the 64-bit frequency in Hz and the range of performance counter
159 If StartValue is not NULL, then the value that the performance counter starts
160 with immediately after is it rolls over is returned in StartValue. If
161 EndValue is not NULL, then the value that the performance counter end with
162 immediately before it rolls over is returned in EndValue. The 64-bit
163 frequency of the performance counter in Hz is always returned. If StartValue
164 is less than EndValue, then the performance counter counts up. If StartValue
165 is greater than EndValue, then the performance counter counts down. For
166 example, a 64-bit free running counter that counts up would have a StartValue
167 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
168 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
170 @param StartValue The value the performance counter starts with when it
172 @param EndValue The value that the performance counter ends with before
175 @return The frequency in Hz.
180 GetPerformanceCounterProperties (
181 OUT UINT64
*StartValue
, OPTIONAL
182 OUT UINT64
*EndValue OPTIONAL
185 if (StartValue
!= NULL
) {
186 // Timer starts with the reload value
187 *StartValue
= (UINT64
)0ULL ;
190 if (EndValue
!= NULL
) {
191 // Timer counts down to 0x0
192 *EndValue
= 0xFFFFFFFFFFFFFFFFUL
;
195 return (UINT64
)ArmGenericTimerGetTimerFreq ();