]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ArmPkg/Library/ArmArchTimerLib/ArmArchTimerLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ArmPkg / Library / ArmArchTimerLib / ArmArchTimerLib.c
... / ...
CommitLineData
1/** @file\r
2 Generic ARM implementation of TimerLib.h\r
3\r
4 Copyright (c) 2011 - 2021, Arm Limited. All rights reserved.<BR>\r
5\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#include <Base.h>\r
11#include <Library/ArmLib.h>\r
12#include <Library/BaseLib.h>\r
13#include <Library/TimerLib.h>\r
14#include <Library/DebugLib.h>\r
15#include <Library/PcdLib.h>\r
16#include <Library/ArmGenericTimerCounterLib.h>\r
17\r
18#define TICKS_PER_MICRO_SEC (PcdGet32 (PcdArmArchTimerFreqInHz)/1000000U)\r
19\r
20// Select appropriate multiply function for platform architecture.\r
21#ifdef MDE_CPU_ARM\r
22#define MULT_U64_X_N MultU64x32\r
23#else\r
24#define MULT_U64_X_N MultU64x64\r
25#endif\r
26\r
27RETURN_STATUS\r
28EFIAPI\r
29TimerConstructor (\r
30 VOID\r
31 )\r
32{\r
33 //\r
34 // Check if the ARM Generic Timer Extension is implemented.\r
35 //\r
36 if (ArmIsArchTimerImplemented ()) {\r
37 //\r
38 // Check if Architectural Timer frequency is pre-determined by the platform\r
39 // (ie. nonzero).\r
40 //\r
41 if (PcdGet32 (PcdArmArchTimerFreqInHz) != 0) {\r
42 //\r
43 // Check if ticks/uS is not 0. The Architectural timer runs at constant\r
44 // frequency, irrespective of CPU frequency. According to Generic Timer\r
45 // Ref manual, lower bound of the frequency is in the range of 1-10MHz.\r
46 //\r
47 ASSERT (TICKS_PER_MICRO_SEC);\r
48\r
49 #ifdef MDE_CPU_ARM\r
50 //\r
51 // Only set the frequency for ARMv7. We expect the secure firmware to\r
52 // have already done it.\r
53 // If the security extension is not implemented, set Timer Frequency\r
54 // here.\r
55 //\r
56 if (ArmHasSecurityExtensions ()) {\r
57 ArmGenericTimerSetTimerFreq (PcdGet32 (PcdArmArchTimerFreqInHz));\r
58 }\r
59\r
60 #endif\r
61 }\r
62\r
63 //\r
64 // Architectural Timer Frequency must be set in Secure privileged\r
65 // mode (if secure extension is supported).\r
66 // If the reset value (0) is returned, just ASSERT.\r
67 //\r
68 ASSERT (ArmGenericTimerGetTimerFreq () != 0);\r
69 } else {\r
70 DEBUG ((DEBUG_ERROR, "ARM Architectural Timer is not available in the CPU, hence this library cannot be used.\n"));\r
71 ASSERT (0);\r
72 }\r
73\r
74 return RETURN_SUCCESS;\r
75}\r
76\r
77/**\r
78 A local utility function that returns the PCD value, if specified.\r
79 Otherwise it defaults to ArmGenericTimerGetTimerFreq.\r
80\r
81 @return The timer frequency.\r
82\r
83**/\r
84STATIC\r
85UINTN\r
86EFIAPI\r
87GetPlatformTimerFreq (\r
88 )\r
89{\r
90 UINTN TimerFreq;\r
91\r
92 TimerFreq = PcdGet32 (PcdArmArchTimerFreqInHz);\r
93 if (TimerFreq == 0) {\r
94 TimerFreq = ArmGenericTimerGetTimerFreq ();\r
95 }\r
96\r
97 return TimerFreq;\r
98}\r
99\r
100/**\r
101 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
102\r
103 @param MicroSeconds The minimum number of microseconds to delay.\r
104\r
105 @return The value of MicroSeconds input.\r
106\r
107**/\r
108UINTN\r
109EFIAPI\r
110MicroSecondDelay (\r
111 IN UINTN MicroSeconds\r
112 )\r
113{\r
114 UINT64 TimerTicks64;\r
115 UINT64 SystemCounterVal;\r
116\r
117 // Calculate counter ticks that represent requested delay:\r
118 // = MicroSeconds x TICKS_PER_MICRO_SEC\r
119 // = MicroSeconds x Frequency.10^-6\r
120 TimerTicks64 = DivU64x32 (\r
121 MULT_U64_X_N (\r
122 MicroSeconds,\r
123 GetPlatformTimerFreq ()\r
124 ),\r
125 1000000U\r
126 );\r
127\r
128 // Read System Counter value\r
129 SystemCounterVal = ArmGenericTimerGetSystemCount ();\r
130\r
131 TimerTicks64 += SystemCounterVal;\r
132\r
133 // Wait until delay count expires.\r
134 while (SystemCounterVal < TimerTicks64) {\r
135 SystemCounterVal = ArmGenericTimerGetSystemCount ();\r
136 }\r
137\r
138 return MicroSeconds;\r
139}\r
140\r
141/**\r
142 Stalls the CPU for at least the given number of nanoseconds.\r
143\r
144 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
145\r
146 When the timer frequency is 1MHz, each tick corresponds to 1 microsecond.\r
147 Therefore, the nanosecond delay will be rounded up to the nearest 1 microsecond.\r
148\r
149 @param NanoSeconds The minimum number of nanoseconds to delay.\r
150\r
151 @return The value of NanoSeconds inputted.\r
152\r
153**/\r
154UINTN\r
155EFIAPI\r
156NanoSecondDelay (\r
157 IN UINTN NanoSeconds\r
158 )\r
159{\r
160 UINTN MicroSeconds;\r
161\r
162 // Round up to 1us Tick Number\r
163 MicroSeconds = NanoSeconds / 1000;\r
164 MicroSeconds += ((NanoSeconds % 1000) == 0) ? 0 : 1;\r
165\r
166 MicroSecondDelay (MicroSeconds);\r
167\r
168 return NanoSeconds;\r
169}\r
170\r
171/**\r
172 Retrieves the current value of a 64-bit free running performance counter.\r
173\r
174 The counter can either count up by 1 or count down by 1. If the physical\r
175 performance counter counts by a larger increment, then the counter values\r
176 must be translated. The properties of the counter can be retrieved from\r
177 GetPerformanceCounterProperties().\r
178\r
179 @return The current value of the free running performance counter.\r
180\r
181**/\r
182UINT64\r
183EFIAPI\r
184GetPerformanceCounter (\r
185 VOID\r
186 )\r
187{\r
188 // Just return the value of system count\r
189 return ArmGenericTimerGetSystemCount ();\r
190}\r
191\r
192/**\r
193 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
194 values.\r
195\r
196 If StartValue is not NULL, then the value that the performance counter starts\r
197 with immediately after is it rolls over is returned in StartValue. If\r
198 EndValue is not NULL, then the value that the performance counter end with\r
199 immediately before it rolls over is returned in EndValue. The 64-bit\r
200 frequency of the performance counter in Hz is always returned. If StartValue\r
201 is less than EndValue, then the performance counter counts up. If StartValue\r
202 is greater than EndValue, then the performance counter counts down. For\r
203 example, a 64-bit free running counter that counts up would have a StartValue\r
204 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
205 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
206\r
207 @param StartValue The value the performance counter starts with when it\r
208 rolls over.\r
209 @param EndValue The value that the performance counter ends with before\r
210 it rolls over.\r
211\r
212 @return The frequency in Hz.\r
213\r
214**/\r
215UINT64\r
216EFIAPI\r
217GetPerformanceCounterProperties (\r
218 OUT UINT64 *StartValue OPTIONAL,\r
219 OUT UINT64 *EndValue OPTIONAL\r
220 )\r
221{\r
222 if (StartValue != NULL) {\r
223 // Timer starts at 0\r
224 *StartValue = (UINT64)0ULL;\r
225 }\r
226\r
227 if (EndValue != NULL) {\r
228 // Timer counts up.\r
229 *EndValue = 0xFFFFFFFFFFFFFFFFUL;\r
230 }\r
231\r
232 return (UINT64)ArmGenericTimerGetTimerFreq ();\r
233}\r
234\r
235/**\r
236 Converts elapsed ticks of performance counter to time in nanoseconds.\r
237\r
238 This function converts the elapsed ticks of running performance counter to\r
239 time value in unit of nanoseconds.\r
240\r
241 @param Ticks The number of elapsed ticks of running performance counter.\r
242\r
243 @return The elapsed time in nanoseconds.\r
244\r
245**/\r
246UINT64\r
247EFIAPI\r
248GetTimeInNanoSecond (\r
249 IN UINT64 Ticks\r
250 )\r
251{\r
252 UINT64 NanoSeconds;\r
253 UINT32 Remainder;\r
254 UINT32 TimerFreq;\r
255\r
256 TimerFreq = GetPlatformTimerFreq ();\r
257 //\r
258 // Ticks\r
259 // Time = --------- x 1,000,000,000\r
260 // Frequency\r
261 //\r
262 NanoSeconds = MULT_U64_X_N (\r
263 DivU64x32Remainder (\r
264 Ticks,\r
265 TimerFreq,\r
266 &Remainder\r
267 ),\r
268 1000000000U\r
269 );\r
270\r
271 //\r
272 // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)\r
273 // will not overflow 64-bit.\r
274 //\r
275 NanoSeconds += DivU64x32 (\r
276 MULT_U64_X_N (\r
277 (UINT64)Remainder,\r
278 1000000000U\r
279 ),\r
280 TimerFreq\r
281 );\r
282\r
283 return NanoSeconds;\r
284}\r