]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/ArmArchTimerLib/ArmArchTimerLib.c
ArmPkg: Add isb when setting SCR
[mirror_edk2.git] / ArmPkg / Library / ArmArchTimerLib / ArmArchTimerLib.c
1 /** @file
2 Generic ARM implementation of TimerLib.h
3
4 Copyright (c) 2011-2014, ARM Limited. All rights reserved.
5
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
10
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.
13
14 **/
15
16
17 #include <Base.h>
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>
24
25 #define TICKS_PER_MICRO_SEC (PcdGet32 (PcdArmArchTimerFreqInHz)/1000000U)
26
27 RETURN_STATUS
28 EFIAPI
29 TimerConstructor (
30 VOID
31 )
32 {
33 //
34 // Check if the ARM Generic Timer Extension is implemented.
35 //
36 if (ArmIsArchTimerImplemented ()) {
37 UINTN TimerFreq;
38
39 //
40 // Check if Architectural Timer frequency is pre-determined by the platform
41 // (ie. nonzero).
42 //
43 if (PcdGet32 (PcdArmArchTimerFreqInHz) != 0) {
44 //
45 // Check if ticks/uS is not 0. The Architectural timer runs at constant
46 // frequency, irrespective of CPU frequency. According to General Timer
47 // Ref manual, lower bound of the frequency is in the range of 1-10MHz.
48 //
49 ASSERT (TICKS_PER_MICRO_SEC);
50
51 #ifdef MDE_CPU_ARM
52 //
53 // Only set the frequency for ARMv7. We expect the secure firmware to
54 // have already done it.
55 // If the security extension is not implemented, set Timer Frequency
56 // here.
57 //
58 if ((ArmReadIdPfr1 () & ARM_PFR1_SEC) == 0x0) {
59 ArmGenericTimerSetTimerFreq (PcdGet32 (PcdArmArchTimerFreqInHz));
60 }
61 #endif
62 }
63
64 //
65 // Architectural Timer Frequency must be set in the Secure privileged
66 // mode (if secure extension is supported).
67 // If the reset value (0) is returned, just ASSERT.
68 //
69 TimerFreq = ArmGenericTimerGetTimerFreq ();
70 ASSERT (TimerFreq != 0);
71 } else {
72 DEBUG ((EFI_D_ERROR, "ARM Architectural Timer is not available in the CPU, hence this library can not be used.\n"));
73 ASSERT (0);
74 }
75
76 return RETURN_SUCCESS;
77 }
78
79
80 /**
81 Stalls the CPU for the number of microseconds specified by MicroSeconds.
82
83 @param MicroSeconds The minimum number of microseconds to delay.
84
85 @return The value of MicroSeconds inputted.
86
87 **/
88 UINTN
89 EFIAPI
90 MicroSecondDelay (
91 IN UINTN MicroSeconds
92 )
93 {
94 UINT64 TimerTicks64;
95 UINT64 SystemCounterVal;
96 UINT64 (EFIAPI
97 *MultU64xN) (
98 IN UINT64 Multiplicand,
99 IN UINTN Multiplier
100 );
101 UINTN TimerFreq;
102
103 #ifdef MDE_CPU_ARM
104 MultU64xN = MultU64x32;
105 #else
106 MultU64xN = MultU64x64;
107 #endif
108
109 TimerFreq = PcdGet32 (PcdArmArchTimerFreqInHz);
110 if (TimerFreq == 0) {
111 TimerFreq = ArmGenericTimerGetTimerFreq ();
112 }
113
114 // Calculate counter ticks that can represent requested delay:
115 // = MicroSeconds x TICKS_PER_MICRO_SEC
116 // = MicroSeconds x Frequency.10^-6
117 TimerTicks64 = DivU64x32 (
118 MultU64xN (
119 MicroSeconds,
120 TimerFreq
121 ),
122 1000000U
123 );
124
125 // Read System Counter value
126 SystemCounterVal = ArmGenericTimerGetSystemCount ();
127
128 TimerTicks64 += SystemCounterVal;
129
130 // Wait until delay count is expired.
131 while (SystemCounterVal < TimerTicks64) {
132 SystemCounterVal = ArmGenericTimerGetSystemCount ();
133 }
134
135 return MicroSeconds;
136 }
137
138
139 /**
140 Stalls the CPU for at least the given number of nanoseconds.
141
142 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
143
144 When the timer frequency is 1MHz, each tick corresponds to 1 microsecond.
145 Therefore, the nanosecond delay will be rounded up to the nearest 1 microsecond.
146
147 @param NanoSeconds The minimum number of nanoseconds to delay.
148
149 @return The value of NanoSeconds inputed.
150
151 **/
152 UINTN
153 EFIAPI
154 NanoSecondDelay (
155 IN UINTN NanoSeconds
156 )
157 {
158 UINTN MicroSeconds;
159
160 // Round up to 1us Tick Number
161 MicroSeconds = NanoSeconds / 1000;
162 MicroSeconds += ((NanoSeconds % 1000) == 0) ? 0 : 1;
163
164 MicroSecondDelay (MicroSeconds);
165
166 return NanoSeconds;
167 }
168
169 /**
170 Retrieves the current value of a 64-bit free running performance counter.
171
172 The counter can either count up by 1 or count down by 1. If the physical
173 performance counter counts by a larger increment, then the counter values
174 must be translated. The properties of the counter can be retrieved from
175 GetPerformanceCounterProperties().
176
177 @return The current value of the free running performance counter.
178
179 **/
180 UINT64
181 EFIAPI
182 GetPerformanceCounter (
183 VOID
184 )
185 {
186 // Just return the value of system count
187 return ArmGenericTimerGetSystemCount ();
188 }
189
190 /**
191 Retrieves the 64-bit frequency in Hz and the range of performance counter
192 values.
193
194 If StartValue is not NULL, then the value that the performance counter starts
195 with immediately after is it rolls over is returned in StartValue. If
196 EndValue is not NULL, then the value that the performance counter end with
197 immediately before it rolls over is returned in EndValue. The 64-bit
198 frequency of the performance counter in Hz is always returned. If StartValue
199 is less than EndValue, then the performance counter counts up. If StartValue
200 is greater than EndValue, then the performance counter counts down. For
201 example, a 64-bit free running counter that counts up would have a StartValue
202 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
203 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
204
205 @param StartValue The value the performance counter starts with when it
206 rolls over.
207 @param EndValue The value that the performance counter ends with before
208 it rolls over.
209
210 @return The frequency in Hz.
211
212 **/
213 UINT64
214 EFIAPI
215 GetPerformanceCounterProperties (
216 OUT UINT64 *StartValue, OPTIONAL
217 OUT UINT64 *EndValue OPTIONAL
218 )
219 {
220 if (StartValue != NULL) {
221 // Timer starts with the reload value
222 *StartValue = (UINT64)0ULL ;
223 }
224
225 if (EndValue != NULL) {
226 // Timer counts down to 0x0
227 *EndValue = 0xFFFFFFFFFFFFFFFFUL;
228 }
229
230 return (UINT64)ArmGenericTimerGetTimerFreq ();
231 }