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