]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/TimerDxe/TimerDxe.c
ArmPkg/TimerDxe: Read timer frequency from CPU
[mirror_edk2.git] / ArmPkg / Drivers / TimerDxe / TimerDxe.c
CommitLineData
1e57a462 1/** @file\r
2 Timer Architecture Protocol driver of the ARM flavor\r
3\r
e703b085
OM
4 Copyright (c) 2011-2013 ARM Ltd. All rights reserved.<BR>\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
1e57a462 10\r
e703b085
OM
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
1e57a462 13\r
14**/\r
15\r
16\r
17#include <PiDxe.h>\r
18\r
19#include <Library/ArmLib.h>\r
20#include <Library/BaseLib.h>\r
21#include <Library/DebugLib.h>\r
22#include <Library/BaseMemoryLib.h>\r
23#include <Library/UefiBootServicesTableLib.h>\r
24#include <Library/UefiLib.h>\r
25#include <Library/PcdLib.h>\r
26#include <Library/IoLib.h>\r
4f6d34b4 27#include <Library/ArmGenericTimerCounterLib.h>\r
d4bb43ce 28#include <Library/ArmArchTimer.h>\r
1e57a462 29\r
30#include <Protocol/Timer.h>\r
31#include <Protocol/HardwareInterrupt.h>\r
32\r
33// The notification function to call on every timer interrupt.\r
34EFI_TIMER_NOTIFY mTimerNotifyFunction = (EFI_TIMER_NOTIFY)NULL;\r
35EFI_EVENT EfiExitBootServicesEvent = (EFI_EVENT)NULL;\r
36\r
37// The current period of the timer interrupt\r
38UINT64 mTimerPeriod = 0;\r
c6c4df80
OM
39// The latest Timer Tick calculated for mTimerPeriod\r
40UINT64 mTimerTicks = 0;\r
41// Number of elapsed period since the last Timer interrupt\r
42UINT64 mElapsedPeriod = 1;\r
1e57a462 43\r
44// Cached copy of the Hardware Interrupt protocol instance\r
45EFI_HARDWARE_INTERRUPT_PROTOCOL *gInterrupt = NULL;\r
46\r
47/**\r
3402aac7
RC
48 This function registers the handler NotifyFunction so it is called every time\r
49 the timer interrupt fires. It also passes the amount of time since the last\r
50 handler call to the NotifyFunction. If NotifyFunction is NULL, then the\r
51 handler is unregistered. If the handler is registered, then EFI_SUCCESS is\r
52 returned. If the CPU does not support registering a timer interrupt handler,\r
53 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler\r
54 when a handler is already registered, then EFI_ALREADY_STARTED is returned.\r
55 If an attempt is made to unregister a handler when a handler is not registered,\r
56 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to\r
57 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR\r
1e57a462 58 is returned.\r
59\r
60 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
61 @param NotifyFunction The function to call when a timer interrupt fires. This\r
62 function executes at TPL_HIGH_LEVEL. The DXE Core will\r
63 register a handler for the timer interrupt, so it can know\r
64 how much time has passed. This information is used to\r
65 signal timer based events. NULL will unregister the handler.\r
66 @retval EFI_SUCCESS The timer handler was registered.\r
67 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.\r
68 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already\r
69 registered.\r
70 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not\r
71 previously registered.\r
72 @retval EFI_DEVICE_ERROR The timer handler could not be registered.\r
73\r
74**/\r
75EFI_STATUS\r
76EFIAPI\r
77TimerDriverRegisterHandler (\r
78 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
79 IN EFI_TIMER_NOTIFY NotifyFunction\r
80 )\r
81{\r
82 if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) {\r
83 return EFI_INVALID_PARAMETER;\r
84 }\r
85\r
86 if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) {\r
87 return EFI_ALREADY_STARTED;\r
88 }\r
89\r
90 mTimerNotifyFunction = NotifyFunction;\r
91\r
92 return EFI_SUCCESS;\r
93}\r
94\r
95/**\r
96 Disable the timer\r
97**/\r
98VOID\r
99EFIAPI\r
100ExitBootServicesEvent (\r
101 IN EFI_EVENT Event,\r
102 IN VOID *Context\r
103 )\r
104{\r
4f6d34b4 105 ArmGenericTimerDisableTimer ();\r
1e57a462 106}\r
107\r
108/**\r
109\r
3402aac7
RC
110 This function adjusts the period of timer interrupts to the value specified\r
111 by TimerPeriod. If the timer period is updated, then the selected timer\r
112 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If\r
113 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.\r
114 If an error occurs while attempting to update the timer period, then the\r
115 timer hardware will be put back in its state prior to this call, and\r
116 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt\r
117 is disabled. This is not the same as disabling the CPU's interrupts.\r
118 Instead, it must either turn off the timer hardware, or it must adjust the\r
119 interrupt controller so that a CPU interrupt is not generated when the timer\r
120 interrupt fires.\r
1e57a462 121\r
122 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
123 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If\r
124 the timer hardware is not programmable, then EFI_UNSUPPORTED is\r
125 returned. If the timer is programmable, then the timer period\r
126 will be rounded up to the nearest timer period that is supported\r
127 by the timer hardware. If TimerPeriod is set to 0, then the\r
128 timer interrupts will be disabled.\r
129\r
130\r
131 @retval EFI_SUCCESS The timer period was changed.\r
132 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.\r
133 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.\r
134\r
135**/\r
136EFI_STATUS\r
137EFIAPI\r
138TimerDriverSetTimerPeriod (\r
139 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
140 IN UINT64 TimerPeriod\r
141 )\r
142{\r
c6c4df80 143 UINT64 CounterValue;\r
1e57a462 144 UINT64 TimerTicks;\r
c6c4df80 145 EFI_TPL OriginalTPL;\r
3402aac7 146\r
1e57a462 147 // Always disable the timer\r
4f6d34b4 148 ArmGenericTimerDisableTimer ();\r
1e57a462 149\r
150 if (TimerPeriod != 0) {\r
c6c4df80
OM
151 // mTimerTicks = TimerPeriod in 1ms unit x Frequency.10^-3\r
152 // = TimerPeriod.10^-4 x Frequency.10^-3\r
153 // = (TimerPeriod x Frequency) x 10^-7\r
7a1e861e 154 TimerTicks = MultU64x32 (TimerPeriod, ArmGenericTimerGetTimerFreq ());\r
33292af5 155 TimerTicks = DivU64x32 (TimerTicks, 10000000U);\r
1e57a462 156\r
c6c4df80
OM
157 // Raise TPL to update the mTimerTicks and mTimerPeriod to ensure these values\r
158 // are coherent in the interrupt handler\r
159 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
160\r
161 mTimerTicks = TimerTicks;\r
162 mTimerPeriod = TimerPeriod;\r
163 mElapsedPeriod = 1;\r
164\r
165 gBS->RestoreTPL (OriginalTPL);\r
166\r
4f6d34b4
AB
167 // Get value of the current timer\r
168 CounterValue = ArmGenericTimerGetSystemCount ();\r
c6c4df80 169 // Set the interrupt in Current Time + mTimerTick\r
4f6d34b4 170 ArmGenericTimerSetCompareVal (CounterValue + mTimerTicks);\r
1e57a462 171\r
172 // Enable the timer\r
4f6d34b4 173 ArmGenericTimerEnableTimer ();\r
c6c4df80
OM
174 } else {\r
175 // Save the new timer period\r
176 mTimerPeriod = TimerPeriod;\r
177 // Reset the elapsed period\r
178 mElapsedPeriod = 1;\r
1e57a462 179 }\r
180\r
1e57a462 181 return EFI_SUCCESS;\r
182}\r
183\r
184/**\r
3402aac7
RC
185 This function retrieves the period of timer interrupts in 100 ns units,\r
186 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod\r
187 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is\r
1e57a462 188 returned, then the timer is currently disabled.\r
189\r
190 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
191 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If\r
192 0 is returned, then the timer is currently disabled.\r
193\r
194\r
195 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.\r
196 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.\r
197\r
198**/\r
199EFI_STATUS\r
200EFIAPI\r
201TimerDriverGetTimerPeriod (\r
202 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
203 OUT UINT64 *TimerPeriod\r
204 )\r
205{\r
206 if (TimerPeriod == NULL) {\r
207 return EFI_INVALID_PARAMETER;\r
208 }\r
209\r
210 *TimerPeriod = mTimerPeriod;\r
211 return EFI_SUCCESS;\r
212}\r
213\r
214/**\r
3402aac7
RC
215 This function generates a soft timer interrupt. If the platform does not support soft\r
216 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.\r
217 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()\r
218 service, then a soft timer interrupt will be generated. If the timer interrupt is\r
219 enabled when this service is called, then the registered handler will be invoked. The\r
220 registered handler should not be able to distinguish a hardware-generated timer\r
1e57a462 221 interrupt from a software-generated timer interrupt.\r
222\r
223 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
224\r
225 @retval EFI_SUCCESS The soft timer interrupt was generated.\r
226 @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.\r
227\r
228**/\r
229EFI_STATUS\r
230EFIAPI\r
231TimerDriverGenerateSoftInterrupt (\r
232 IN EFI_TIMER_ARCH_PROTOCOL *This\r
233 )\r
234{\r
235 return EFI_UNSUPPORTED;\r
236}\r
237\r
238/**\r
239 Interface structure for the Timer Architectural Protocol.\r
240\r
241 @par Protocol Description:\r
242 This protocol provides the services to initialize a periodic timer\r
243 interrupt, and to register a handler that is called each time the timer\r
244 interrupt fires. It may also provide a service to adjust the rate of the\r
245 periodic timer interrupt. When a timer interrupt occurs, the handler is\r
246 passed the amount of time that has passed since the previous timer\r
247 interrupt.\r
248\r
249 @param RegisterHandler\r
250 Registers a handler that will be called each time the\r
251 timer interrupt fires. TimerPeriod defines the minimum\r
252 time between timer interrupts, so TimerPeriod will also\r
253 be the minimum time between calls to the registered\r
254 handler.\r
255\r
256 @param SetTimerPeriod\r
257 Sets the period of the timer interrupt in 100 nS units.\r
258 This function is optional, and may return EFI_UNSUPPORTED.\r
259 If this function is supported, then the timer period will\r
260 be rounded up to the nearest supported timer period.\r
261\r
262\r
263 @param GetTimerPeriod\r
264 Retrieves the period of the timer interrupt in 100 nS units.\r
265\r
266 @param GenerateSoftInterrupt\r
267 Generates a soft timer interrupt that simulates the firing of\r
268 the timer interrupt. This service can be used to invoke the registered handler if the timer interrupt has been masked for\r
269 a period of time.\r
270\r
271**/\r
272EFI_TIMER_ARCH_PROTOCOL gTimer = {\r
273 TimerDriverRegisterHandler,\r
274 TimerDriverSetTimerPeriod,\r
275 TimerDriverGetTimerPeriod,\r
276 TimerDriverGenerateSoftInterrupt\r
277};\r
278\r
279/**\r
280\r
281 C Interrupt Handler called in the interrupt context when Source interrupt is active.\r
282\r
283\r
284 @param Source Source of the interrupt. Hardware routing off a specific platform defines\r
285 what source means.\r
286\r
287 @param SystemContext Pointer to system register context. Mostly used by debuggers and will\r
288 update the system context after the return from the interrupt if\r
289 modified. Don't change these values unless you know what you are doing\r
290\r
291**/\r
292VOID\r
293EFIAPI\r
294TimerInterruptHandler (\r
295 IN HARDWARE_INTERRUPT_SOURCE Source,\r
296 IN EFI_SYSTEM_CONTEXT SystemContext\r
297 )\r
298{\r
299 EFI_TPL OriginalTPL;\r
c6c4df80
OM
300 UINT64 CurrentValue;\r
301 UINT64 CompareValue;\r
1e57a462 302\r
303 //\r
304 // DXE core uses this callback for the EFI timer tick. The DXE core uses locks\r
305 // that raise to TPL_HIGH and then restore back to current level. Thus we need\r
306 // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick.\r
307 //\r
308 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
309\r
310 // Check if the timer interrupt is active\r
4f6d34b4 311 if ((ArmGenericTimerGetTimerCtrlReg () ) & ARM_ARCH_TIMER_ISTATUS) {\r
1e57a462 312\r
313 // Signal end of interrupt early to help avoid losing subsequent ticks from long duration handlers\r
314 gInterrupt->EndOfInterrupt (gInterrupt, Source);\r
315\r
316 if (mTimerNotifyFunction) {\r
c6c4df80 317 mTimerNotifyFunction (mTimerPeriod * mElapsedPeriod);\r
1e57a462 318 }\r
319\r
c6c4df80 320 //\r
1e57a462 321 // Reload the Timer\r
c6c4df80
OM
322 //\r
323\r
324 // Get current counter value\r
4f6d34b4 325 CurrentValue = ArmGenericTimerGetSystemCount ();\r
c6c4df80 326 // Get the counter value to compare with\r
4f6d34b4 327 CompareValue = ArmGenericTimerGetCompareVal ();\r
c6c4df80
OM
328\r
329 // This loop is needed in case we missed interrupts (eg: case when the interrupt handling\r
330 // has taken longer than mTickPeriod).\r
331 // Note: Physical Counter is counting up\r
332 mElapsedPeriod = 0;\r
333 do {\r
334 CompareValue += mTimerTicks;\r
335 mElapsedPeriod++;\r
336 } while (CompareValue < CurrentValue);\r
337\r
338 // Set next compare value\r
4f6d34b4 339 ArmGenericTimerSetCompareVal (CompareValue);\r
1e57a462 340 }\r
341\r
342 // Enable timer interrupts\r
343 gInterrupt->EnableInterruptSource (gInterrupt, Source);\r
344\r
345 gBS->RestoreTPL (OriginalTPL);\r
346}\r
347\r
348\r
349/**\r
350 Initialize the state information for the Timer Architectural Protocol and\r
351 the Timer Debug support protocol that allows the debugger to break into a\r
352 running program.\r
353\r
354 @param ImageHandle of the loaded driver\r
355 @param SystemTable Pointer to the System Table\r
356\r
357 @retval EFI_SUCCESS Protocol registered\r
358 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure\r
359 @retval EFI_DEVICE_ERROR Hardware problems\r
360\r
361**/\r
362EFI_STATUS\r
363EFIAPI\r
364TimerInitialize (\r
365 IN EFI_HANDLE ImageHandle,\r
366 IN EFI_SYSTEM_TABLE *SystemTable\r
367 )\r
368{\r
369 EFI_HANDLE Handle = NULL;\r
370 EFI_STATUS Status;\r
371 UINTN TimerCtrlReg;\r
372\r
373 if (ArmIsArchTimerImplemented () == 0) {\r
374 DEBUG ((EFI_D_ERROR, "ARM Architectural Timer is not available in the CPU, hence cann't use this Driver \n"));\r
375 ASSERT (0);\r
376 }\r
377\r
378 // Find the interrupt controller protocol. ASSERT if not found.\r
379 Status = gBS->LocateProtocol (&gHardwareInterruptProtocolGuid, NULL, (VOID **)&gInterrupt);\r
380 ASSERT_EFI_ERROR (Status);\r
381\r
382 // Disable the timer\r
4f6d34b4 383 TimerCtrlReg = ArmGenericTimerGetTimerCtrlReg ();\r
e703b085
OM
384 TimerCtrlReg |= ARM_ARCH_TIMER_IMASK;\r
385 TimerCtrlReg &= ~ARM_ARCH_TIMER_ENABLE;\r
4f6d34b4 386 ArmGenericTimerSetTimerCtrlReg (TimerCtrlReg);\r
1e57a462 387 Status = TimerDriverSetTimerPeriod (&gTimer, 0);\r
388 ASSERT_EFI_ERROR (Status);\r
389\r
390 // Install secure and Non-secure interrupt handlers\r
391 // Note: Because it is not possible to determine the security state of the\r
392 // CPU dynamically, we just install interrupt handler for both sec and non-sec\r
393 // timer PPI\r
2785509b
AB
394 Status = gInterrupt->RegisterInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerVirtIntrNum), TimerInterruptHandler);\r
395 ASSERT_EFI_ERROR (Status);\r
396\r
397 Status = gInterrupt->RegisterInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerHypIntrNum), TimerInterruptHandler);\r
398 ASSERT_EFI_ERROR (Status);\r
399\r
1e57a462 400 Status = gInterrupt->RegisterInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerSecIntrNum), TimerInterruptHandler);\r
401 ASSERT_EFI_ERROR (Status);\r
402\r
403 Status = gInterrupt->RegisterInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerIntrNum), TimerInterruptHandler);\r
404 ASSERT_EFI_ERROR (Status);\r
405\r
1e57a462 406 // Set up default timer\r
407 Status = TimerDriverSetTimerPeriod (&gTimer, FixedPcdGet32(PcdTimerPeriod)); // TIMER_DEFAULT_PERIOD\r
408 ASSERT_EFI_ERROR (Status);\r
409\r
410 // Install the Timer Architectural Protocol onto a new handle\r
411 Status = gBS->InstallMultipleProtocolInterfaces(\r
412 &Handle,\r
413 &gEfiTimerArchProtocolGuid, &gTimer,\r
414 NULL\r
415 );\r
416 ASSERT_EFI_ERROR(Status);\r
417\r
e703b085
OM
418 // Everything is ready, unmask and enable timer interrupts\r
419 TimerCtrlReg = ARM_ARCH_TIMER_ENABLE;\r
4f6d34b4 420 ArmGenericTimerSetTimerCtrlReg (TimerCtrlReg);\r
1e57a462 421\r
422 // Register for an ExitBootServicesEvent\r
423 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY, ExitBootServicesEvent, NULL, &EfiExitBootServicesEvent);\r
424 ASSERT_EFI_ERROR (Status);\r
425\r
426 return Status;\r
427}\r