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