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