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