]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmArchTimerLib/ArmArchTimerLib.c
CorebootModulePkg: Reformat asm constant to avoid gcc assembler fail
[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 39 //\r
16b7aff0
LE
40 // Check if Architectural Timer frequency is pre-determined by the platform\r
41 // (ie. nonzero).\r
074a67fc 42 //\r
16b7aff0
LE
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
da9675a2 50\r
25402f5d 51#ifdef MDE_CPU_ARM\r
16b7aff0
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
58 if ((ArmReadIdPfr1 () & ARM_PFR1_SEC) == 0x0) {\r
59 ArmGenericTimerSetTimerFreq (PcdGet32 (PcdArmArchTimerFreqInHz));\r
60 }\r
25402f5d 61#endif\r
16b7aff0 62 }\r
da9675a2 63\r
074a67fc
LE
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
4f6d34b4 69 TimerFreq = ArmGenericTimerGetTimerFreq ();\r
25402f5d 70 ASSERT (TimerFreq != 0);\r
da9675a2 71 } else {\r
ce88684e 72 DEBUG ((EFI_D_ERROR, "ARM Architectural Timer is not available in the CPU, hence this library can not be used.\n"));\r
da9675a2 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
16b7aff0
LE
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
da9675a2 113\r
33292af5
OM
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
b36bc5af 117 TimerTicks64 = DivU64x32 (\r
16b7aff0 118 MultU64xN (\r
b36bc5af 119 MicroSeconds,\r
16b7aff0 120 TimerFreq\r
b36bc5af
LE
121 ),\r
122 1000000U\r
123 );\r
da9675a2 124\r
125 // Read System Counter value\r
4f6d34b4 126 SystemCounterVal = ArmGenericTimerGetSystemCount ();\r
da9675a2 127\r
128 TimerTicks64 += SystemCounterVal;\r
129\r
130 // Wait until delay count is expired.\r
131 while (SystemCounterVal < TimerTicks64) {\r
4f6d34b4 132 SystemCounterVal = ArmGenericTimerGetSystemCount ();\r
da9675a2 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
ce88684e 149 @return The value of NanoSeconds inputed.\r
da9675a2 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
4f6d34b4 187 return ArmGenericTimerGetSystemCount ();\r
da9675a2 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
89bbce11 227 *EndValue = 0xFFFFFFFFFFFFFFFFUL;\r
da9675a2 228 }\r
229\r
4f6d34b4 230 return (UINT64)ArmGenericTimerGetTimerFreq ();\r
da9675a2 231}\r