2 Timer Architecture Protocol driver of the ARM flavor
4 Copyright (c) 2011-2013 ARM Ltd. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/ArmLib.h>
14 #include <Library/BaseLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/UefiLib.h>
19 #include <Library/PcdLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/ArmGenericTimerCounterLib.h>
23 #include <Protocol/Timer.h>
24 #include <Protocol/HardwareInterrupt.h>
26 // The notification function to call on every timer interrupt.
27 EFI_TIMER_NOTIFY mTimerNotifyFunction
= (EFI_TIMER_NOTIFY
)NULL
;
28 EFI_EVENT EfiExitBootServicesEvent
= (EFI_EVENT
)NULL
;
30 // The current period of the timer interrupt
31 UINT64 mTimerPeriod
= 0;
32 // The latest Timer Tick calculated for mTimerPeriod
33 UINT64 mTimerTicks
= 0;
34 // Number of elapsed period since the last Timer interrupt
35 UINT64 mElapsedPeriod
= 1;
37 // Cached copy of the Hardware Interrupt protocol instance
38 EFI_HARDWARE_INTERRUPT_PROTOCOL
*gInterrupt
= NULL
;
41 This function registers the handler NotifyFunction so it is called every time
42 the timer interrupt fires. It also passes the amount of time since the last
43 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
44 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
45 returned. If the CPU does not support registering a timer interrupt handler,
46 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
47 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
48 If an attempt is made to unregister a handler when a handler is not registered,
49 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
50 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
53 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
54 @param NotifyFunction The function to call when a timer interrupt fires. This
55 function executes at TPL_HIGH_LEVEL. The DXE Core will
56 register a handler for the timer interrupt, so it can know
57 how much time has passed. This information is used to
58 signal timer based events. NULL will unregister the handler.
59 @retval EFI_SUCCESS The timer handler was registered.
60 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
61 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
63 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
64 previously registered.
65 @retval EFI_DEVICE_ERROR The timer handler could not be registered.
70 TimerDriverRegisterHandler (
71 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
72 IN EFI_TIMER_NOTIFY NotifyFunction
75 if ((NotifyFunction
== NULL
) && (mTimerNotifyFunction
== NULL
)) {
76 return EFI_INVALID_PARAMETER
;
79 if ((NotifyFunction
!= NULL
) && (mTimerNotifyFunction
!= NULL
)) {
80 return EFI_ALREADY_STARTED
;
83 mTimerNotifyFunction
= NotifyFunction
;
93 ExitBootServicesEvent (
98 ArmGenericTimerDisableTimer ();
103 This function adjusts the period of timer interrupts to the value specified
104 by TimerPeriod. If the timer period is updated, then the selected timer
105 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
106 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
107 If an error occurs while attempting to update the timer period, then the
108 timer hardware will be put back in its state prior to this call, and
109 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
110 is disabled. This is not the same as disabling the CPU's interrupts.
111 Instead, it must either turn off the timer hardware, or it must adjust the
112 interrupt controller so that a CPU interrupt is not generated when the timer
115 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
116 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
117 the timer hardware is not programmable, then EFI_UNSUPPORTED is
118 returned. If the timer is programmable, then the timer period
119 will be rounded up to the nearest timer period that is supported
120 by the timer hardware. If TimerPeriod is set to 0, then the
121 timer interrupts will be disabled.
124 @retval EFI_SUCCESS The timer period was changed.
125 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
126 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
131 TimerDriverSetTimerPeriod (
132 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
133 IN UINT64 TimerPeriod
140 // Always disable the timer
141 ArmGenericTimerDisableTimer ();
143 if (TimerPeriod
!= 0) {
144 // mTimerTicks = TimerPeriod in 1ms unit x Frequency.10^-3
145 // = TimerPeriod.10^-4 x Frequency.10^-3
146 // = (TimerPeriod x Frequency) x 10^-7
147 TimerTicks
= MultU64x32 (TimerPeriod
, ArmGenericTimerGetTimerFreq ());
148 TimerTicks
= DivU64x32 (TimerTicks
, 10000000U);
150 // Raise TPL to update the mTimerTicks and mTimerPeriod to ensure these values
151 // are coherent in the interrupt handler
152 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
154 mTimerTicks
= TimerTicks
;
155 mTimerPeriod
= TimerPeriod
;
158 gBS
->RestoreTPL (OriginalTPL
);
160 // Get value of the current timer
161 CounterValue
= ArmGenericTimerGetSystemCount ();
162 // Set the interrupt in Current Time + mTimerTick
163 ArmGenericTimerSetCompareVal (CounterValue
+ mTimerTicks
);
166 ArmGenericTimerEnableTimer ();
168 // Save the new timer period
169 mTimerPeriod
= TimerPeriod
;
170 // Reset the elapsed period
178 This function retrieves the period of timer interrupts in 100 ns units,
179 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
180 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
181 returned, then the timer is currently disabled.
183 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
184 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
185 0 is returned, then the timer is currently disabled.
188 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
189 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
194 TimerDriverGetTimerPeriod (
195 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
196 OUT UINT64
*TimerPeriod
199 if (TimerPeriod
== NULL
) {
200 return EFI_INVALID_PARAMETER
;
203 *TimerPeriod
= mTimerPeriod
;
208 This function generates a soft timer interrupt. If the platform does not support soft
209 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
210 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
211 service, then a soft timer interrupt will be generated. If the timer interrupt is
212 enabled when this service is called, then the registered handler will be invoked. The
213 registered handler should not be able to distinguish a hardware-generated timer
214 interrupt from a software-generated timer interrupt.
216 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
218 @retval EFI_SUCCESS The soft timer interrupt was generated.
219 @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
224 TimerDriverGenerateSoftInterrupt (
225 IN EFI_TIMER_ARCH_PROTOCOL
*This
228 return EFI_UNSUPPORTED
;
232 Interface structure for the Timer Architectural Protocol.
234 @par Protocol Description:
235 This protocol provides the services to initialize a periodic timer
236 interrupt, and to register a handler that is called each time the timer
237 interrupt fires. It may also provide a service to adjust the rate of the
238 periodic timer interrupt. When a timer interrupt occurs, the handler is
239 passed the amount of time that has passed since the previous timer
242 @param RegisterHandler
243 Registers a handler that will be called each time the
244 timer interrupt fires. TimerPeriod defines the minimum
245 time between timer interrupts, so TimerPeriod will also
246 be the minimum time between calls to the registered
249 @param SetTimerPeriod
250 Sets the period of the timer interrupt in 100 nS units.
251 This function is optional, and may return EFI_UNSUPPORTED.
252 If this function is supported, then the timer period will
253 be rounded up to the nearest supported timer period.
256 @param GetTimerPeriod
257 Retrieves the period of the timer interrupt in 100 nS units.
259 @param GenerateSoftInterrupt
260 Generates a soft timer interrupt that simulates the firing of
261 the timer interrupt. This service can be used to invoke the registered handler if the timer interrupt has been masked for
265 EFI_TIMER_ARCH_PROTOCOL gTimer
= {
266 TimerDriverRegisterHandler
,
267 TimerDriverSetTimerPeriod
,
268 TimerDriverGetTimerPeriod
,
269 TimerDriverGenerateSoftInterrupt
274 C Interrupt Handler called in the interrupt context when Source interrupt is active.
277 @param Source Source of the interrupt. Hardware routing off a specific platform defines
280 @param SystemContext Pointer to system register context. Mostly used by debuggers and will
281 update the system context after the return from the interrupt if
282 modified. Don't change these values unless you know what you are doing
287 TimerInterruptHandler (
288 IN HARDWARE_INTERRUPT_SOURCE Source
,
289 IN EFI_SYSTEM_CONTEXT SystemContext
297 // DXE core uses this callback for the EFI timer tick. The DXE core uses locks
298 // that raise to TPL_HIGH and then restore back to current level. Thus we need
299 // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick.
301 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
303 // Signal end of interrupt early to help avoid losing subsequent ticks
304 // from long duration handlers
305 gInterrupt
->EndOfInterrupt (gInterrupt
, Source
);
307 // Check if the timer interrupt is active
308 if ((ArmGenericTimerGetTimerCtrlReg () ) & ARM_ARCH_TIMER_ISTATUS
) {
310 if (mTimerNotifyFunction
) {
311 mTimerNotifyFunction (mTimerPeriod
* mElapsedPeriod
);
318 // Get current counter value
319 CurrentValue
= ArmGenericTimerGetSystemCount ();
320 // Get the counter value to compare with
321 CompareValue
= ArmGenericTimerGetCompareVal ();
323 // This loop is needed in case we missed interrupts (eg: case when the interrupt handling
324 // has taken longer than mTickPeriod).
325 // Note: Physical Counter is counting up
328 CompareValue
+= mTimerTicks
;
330 } while (CompareValue
< CurrentValue
);
332 // Set next compare value
333 ArmGenericTimerSetCompareVal (CompareValue
);
334 ArmGenericTimerReenableTimer ();
335 ArmInstructionSynchronizationBarrier ();
338 gBS
->RestoreTPL (OriginalTPL
);
343 Initialize the state information for the Timer Architectural Protocol and
344 the Timer Debug support protocol that allows the debugger to break into a
347 @param ImageHandle of the loaded driver
348 @param SystemTable Pointer to the System Table
350 @retval EFI_SUCCESS Protocol registered
351 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
352 @retval EFI_DEVICE_ERROR Hardware problems
358 IN EFI_HANDLE ImageHandle
,
359 IN EFI_SYSTEM_TABLE
*SystemTable
362 EFI_HANDLE Handle
= NULL
;
365 UINT32 TimerHypIntrNum
;
367 if (ArmIsArchTimerImplemented () == 0) {
368 DEBUG ((EFI_D_ERROR
, "ARM Architectural Timer is not available in the CPU, hence cann't use this Driver \n"));
372 // Find the interrupt controller protocol. ASSERT if not found.
373 Status
= gBS
->LocateProtocol (&gHardwareInterruptProtocolGuid
, NULL
, (VOID
**)&gInterrupt
);
374 ASSERT_EFI_ERROR (Status
);
377 TimerCtrlReg
= ArmGenericTimerGetTimerCtrlReg ();
378 TimerCtrlReg
|= ARM_ARCH_TIMER_IMASK
;
379 TimerCtrlReg
&= ~ARM_ARCH_TIMER_ENABLE
;
380 ArmGenericTimerSetTimerCtrlReg (TimerCtrlReg
);
381 Status
= TimerDriverSetTimerPeriod (&gTimer
, 0);
382 ASSERT_EFI_ERROR (Status
);
384 // Install secure and Non-secure interrupt handlers
385 // Note: Because it is not possible to determine the security state of the
386 // CPU dynamically, we just install interrupt handler for both sec and non-sec
388 Status
= gInterrupt
->RegisterInterruptSource (gInterrupt
, PcdGet32 (PcdArmArchTimerVirtIntrNum
), TimerInterruptHandler
);
389 ASSERT_EFI_ERROR (Status
);
392 // The hypervisor timer interrupt may be omitted by implementations that
393 // execute under virtualization.
395 TimerHypIntrNum
= PcdGet32 (PcdArmArchTimerHypIntrNum
);
396 if (TimerHypIntrNum
!= 0) {
397 Status
= gInterrupt
->RegisterInterruptSource (gInterrupt
, TimerHypIntrNum
, TimerInterruptHandler
);
398 ASSERT_EFI_ERROR (Status
);
401 Status
= gInterrupt
->RegisterInterruptSource (gInterrupt
, PcdGet32 (PcdArmArchTimerSecIntrNum
), TimerInterruptHandler
);
402 ASSERT_EFI_ERROR (Status
);
404 Status
= gInterrupt
->RegisterInterruptSource (gInterrupt
, PcdGet32 (PcdArmArchTimerIntrNum
), TimerInterruptHandler
);
405 ASSERT_EFI_ERROR (Status
);
407 // Set up default timer
408 Status
= TimerDriverSetTimerPeriod (&gTimer
, FixedPcdGet32(PcdTimerPeriod
)); // TIMER_DEFAULT_PERIOD
409 ASSERT_EFI_ERROR (Status
);
411 // Install the Timer Architectural Protocol onto a new handle
412 Status
= gBS
->InstallMultipleProtocolInterfaces(
414 &gEfiTimerArchProtocolGuid
, &gTimer
,
417 ASSERT_EFI_ERROR(Status
);
419 // Everything is ready, unmask and enable timer interrupts
420 TimerCtrlReg
= ARM_ARCH_TIMER_ENABLE
;
421 ArmGenericTimerSetTimerCtrlReg (TimerCtrlReg
);
423 // Register for an ExitBootServicesEvent
424 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_NOTIFY
, ExitBootServicesEvent
, NULL
, &EfiExitBootServicesEvent
);
425 ASSERT_EFI_ERROR (Status
);