2 Timer Architecture Protocol driver of the ARM flavor
4 Copyright (c) 2011-2013 ARM Ltd. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Library/ArmLib.h>
20 #include <Library/BaseLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/IoLib.h>
27 #include <Library/ArmGenericTimerCounterLib.h>
28 #include <Library/ArmArchTimer.h>
30 #include <Protocol/Timer.h>
31 #include <Protocol/HardwareInterrupt.h>
33 // The notification function to call on every timer interrupt.
34 EFI_TIMER_NOTIFY mTimerNotifyFunction
= (EFI_TIMER_NOTIFY
)NULL
;
35 EFI_EVENT EfiExitBootServicesEvent
= (EFI_EVENT
)NULL
;
37 // The current period of the timer interrupt
38 UINT64 mTimerPeriod
= 0;
39 // The latest Timer Tick calculated for mTimerPeriod
40 UINT64 mTimerTicks
= 0;
41 // Number of elapsed period since the last Timer interrupt
42 UINT64 mElapsedPeriod
= 1;
44 // Cached copy of the Hardware Interrupt protocol instance
45 EFI_HARDWARE_INTERRUPT_PROTOCOL
*gInterrupt
= NULL
;
48 This function registers the handler NotifyFunction so it is called every time
49 the timer interrupt fires. It also passes the amount of time since the last
50 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
51 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
52 returned. If the CPU does not support registering a timer interrupt handler,
53 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
54 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
55 If an attempt is made to unregister a handler when a handler is not registered,
56 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
57 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
60 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
61 @param NotifyFunction The function to call when a timer interrupt fires. This
62 function executes at TPL_HIGH_LEVEL. The DXE Core will
63 register a handler for the timer interrupt, so it can know
64 how much time has passed. This information is used to
65 signal timer based events. NULL will unregister the handler.
66 @retval EFI_SUCCESS The timer handler was registered.
67 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
68 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
70 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
71 previously registered.
72 @retval EFI_DEVICE_ERROR The timer handler could not be registered.
77 TimerDriverRegisterHandler (
78 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
79 IN EFI_TIMER_NOTIFY NotifyFunction
82 if ((NotifyFunction
== NULL
) && (mTimerNotifyFunction
== NULL
)) {
83 return EFI_INVALID_PARAMETER
;
86 if ((NotifyFunction
!= NULL
) && (mTimerNotifyFunction
!= NULL
)) {
87 return EFI_ALREADY_STARTED
;
90 mTimerNotifyFunction
= NotifyFunction
;
100 ExitBootServicesEvent (
105 ArmGenericTimerDisableTimer ();
110 This function adjusts the period of timer interrupts to the value specified
111 by TimerPeriod. If the timer period is updated, then the selected timer
112 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
113 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
114 If an error occurs while attempting to update the timer period, then the
115 timer hardware will be put back in its state prior to this call, and
116 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
117 is disabled. This is not the same as disabling the CPU's interrupts.
118 Instead, it must either turn off the timer hardware, or it must adjust the
119 interrupt controller so that a CPU interrupt is not generated when the timer
122 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
123 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
124 the timer hardware is not programmable, then EFI_UNSUPPORTED is
125 returned. If the timer is programmable, then the timer period
126 will be rounded up to the nearest timer period that is supported
127 by the timer hardware. If TimerPeriod is set to 0, then the
128 timer interrupts will be disabled.
131 @retval EFI_SUCCESS The timer period was changed.
132 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
133 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
138 TimerDriverSetTimerPeriod (
139 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
140 IN UINT64 TimerPeriod
147 // Always disable the timer
148 ArmGenericTimerDisableTimer ();
150 if (TimerPeriod
!= 0) {
151 // mTimerTicks = TimerPeriod in 1ms unit x Frequency.10^-3
152 // = TimerPeriod.10^-4 x Frequency.10^-3
153 // = (TimerPeriod x Frequency) x 10^-7
154 TimerTicks
= MultU64x32 (TimerPeriod
, ArmGenericTimerGetTimerFreq ());
155 TimerTicks
= DivU64x32 (TimerTicks
, 10000000U);
157 // Raise TPL to update the mTimerTicks and mTimerPeriod to ensure these values
158 // are coherent in the interrupt handler
159 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
161 mTimerTicks
= TimerTicks
;
162 mTimerPeriod
= TimerPeriod
;
165 gBS
->RestoreTPL (OriginalTPL
);
167 // Get value of the current timer
168 CounterValue
= ArmGenericTimerGetSystemCount ();
169 // Set the interrupt in Current Time + mTimerTick
170 ArmGenericTimerSetCompareVal (CounterValue
+ mTimerTicks
);
173 ArmGenericTimerEnableTimer ();
175 // Save the new timer period
176 mTimerPeriod
= TimerPeriod
;
177 // Reset the elapsed period
185 This function retrieves the period of timer interrupts in 100 ns units,
186 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
187 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
188 returned, then the timer is currently disabled.
190 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
191 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
192 0 is returned, then the timer is currently disabled.
195 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
196 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
201 TimerDriverGetTimerPeriod (
202 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
203 OUT UINT64
*TimerPeriod
206 if (TimerPeriod
== NULL
) {
207 return EFI_INVALID_PARAMETER
;
210 *TimerPeriod
= mTimerPeriod
;
215 This function generates a soft timer interrupt. If the platform does not support soft
216 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
217 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
218 service, then a soft timer interrupt will be generated. If the timer interrupt is
219 enabled when this service is called, then the registered handler will be invoked. The
220 registered handler should not be able to distinguish a hardware-generated timer
221 interrupt from a software-generated timer interrupt.
223 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
225 @retval EFI_SUCCESS The soft timer interrupt was generated.
226 @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
231 TimerDriverGenerateSoftInterrupt (
232 IN EFI_TIMER_ARCH_PROTOCOL
*This
235 return EFI_UNSUPPORTED
;
239 Interface structure for the Timer Architectural Protocol.
241 @par Protocol Description:
242 This protocol provides the services to initialize a periodic timer
243 interrupt, and to register a handler that is called each time the timer
244 interrupt fires. It may also provide a service to adjust the rate of the
245 periodic timer interrupt. When a timer interrupt occurs, the handler is
246 passed the amount of time that has passed since the previous timer
249 @param RegisterHandler
250 Registers a handler that will be called each time the
251 timer interrupt fires. TimerPeriod defines the minimum
252 time between timer interrupts, so TimerPeriod will also
253 be the minimum time between calls to the registered
256 @param SetTimerPeriod
257 Sets the period of the timer interrupt in 100 nS units.
258 This function is optional, and may return EFI_UNSUPPORTED.
259 If this function is supported, then the timer period will
260 be rounded up to the nearest supported timer period.
263 @param GetTimerPeriod
264 Retrieves the period of the timer interrupt in 100 nS units.
266 @param GenerateSoftInterrupt
267 Generates a soft timer interrupt that simulates the firing of
268 the timer interrupt. This service can be used to invoke the registered handler if the timer interrupt has been masked for
272 EFI_TIMER_ARCH_PROTOCOL gTimer
= {
273 TimerDriverRegisterHandler
,
274 TimerDriverSetTimerPeriod
,
275 TimerDriverGetTimerPeriod
,
276 TimerDriverGenerateSoftInterrupt
281 C Interrupt Handler called in the interrupt context when Source interrupt is active.
284 @param Source Source of the interrupt. Hardware routing off a specific platform defines
287 @param SystemContext Pointer to system register context. Mostly used by debuggers and will
288 update the system context after the return from the interrupt if
289 modified. Don't change these values unless you know what you are doing
294 TimerInterruptHandler (
295 IN HARDWARE_INTERRUPT_SOURCE Source
,
296 IN EFI_SYSTEM_CONTEXT SystemContext
304 // DXE core uses this callback for the EFI timer tick. The DXE core uses locks
305 // that raise to TPL_HIGH and then restore back to current level. Thus we need
306 // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick.
308 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
310 // Check if the timer interrupt is active
311 if ((ArmGenericTimerGetTimerCtrlReg () ) & ARM_ARCH_TIMER_ISTATUS
) {
313 // Signal end of interrupt early to help avoid losing subsequent ticks from long duration handlers
314 gInterrupt
->EndOfInterrupt (gInterrupt
, Source
);
316 if (mTimerNotifyFunction
) {
317 mTimerNotifyFunction (mTimerPeriod
* mElapsedPeriod
);
324 // Get current counter value
325 CurrentValue
= ArmGenericTimerGetSystemCount ();
326 // Get the counter value to compare with
327 CompareValue
= ArmGenericTimerGetCompareVal ();
329 // This loop is needed in case we missed interrupts (eg: case when the interrupt handling
330 // has taken longer than mTickPeriod).
331 // Note: Physical Counter is counting up
334 CompareValue
+= mTimerTicks
;
336 } while (CompareValue
< CurrentValue
);
338 // Set next compare value
339 ArmGenericTimerSetCompareVal (CompareValue
);
340 ArmGenericTimerEnableTimer ();
343 // Enable timer interrupts
344 gInterrupt
->EnableInterruptSource (gInterrupt
, Source
);
346 gBS
->RestoreTPL (OriginalTPL
);
351 Initialize the state information for the Timer Architectural Protocol and
352 the Timer Debug support protocol that allows the debugger to break into a
355 @param ImageHandle of the loaded driver
356 @param SystemTable Pointer to the System Table
358 @retval EFI_SUCCESS Protocol registered
359 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
360 @retval EFI_DEVICE_ERROR Hardware problems
366 IN EFI_HANDLE ImageHandle
,
367 IN EFI_SYSTEM_TABLE
*SystemTable
370 EFI_HANDLE Handle
= NULL
;
373 UINT32 TimerHypIntrNum
;
375 if (ArmIsArchTimerImplemented () == 0) {
376 DEBUG ((EFI_D_ERROR
, "ARM Architectural Timer is not available in the CPU, hence cann't use this Driver \n"));
380 // Find the interrupt controller protocol. ASSERT if not found.
381 Status
= gBS
->LocateProtocol (&gHardwareInterruptProtocolGuid
, NULL
, (VOID
**)&gInterrupt
);
382 ASSERT_EFI_ERROR (Status
);
385 TimerCtrlReg
= ArmGenericTimerGetTimerCtrlReg ();
386 TimerCtrlReg
|= ARM_ARCH_TIMER_IMASK
;
387 TimerCtrlReg
&= ~ARM_ARCH_TIMER_ENABLE
;
388 ArmGenericTimerSetTimerCtrlReg (TimerCtrlReg
);
389 Status
= TimerDriverSetTimerPeriod (&gTimer
, 0);
390 ASSERT_EFI_ERROR (Status
);
392 // Install secure and Non-secure interrupt handlers
393 // Note: Because it is not possible to determine the security state of the
394 // CPU dynamically, we just install interrupt handler for both sec and non-sec
396 Status
= gInterrupt
->RegisterInterruptSource (gInterrupt
, PcdGet32 (PcdArmArchTimerVirtIntrNum
), TimerInterruptHandler
);
397 ASSERT_EFI_ERROR (Status
);
400 // The hypervisor timer interrupt may be omitted by implementations that
401 // execute under virtualization.
403 TimerHypIntrNum
= PcdGet32 (PcdArmArchTimerHypIntrNum
);
404 if (TimerHypIntrNum
!= 0) {
405 Status
= gInterrupt
->RegisterInterruptSource (gInterrupt
, TimerHypIntrNum
, TimerInterruptHandler
);
406 ASSERT_EFI_ERROR (Status
);
409 Status
= gInterrupt
->RegisterInterruptSource (gInterrupt
, PcdGet32 (PcdArmArchTimerSecIntrNum
), TimerInterruptHandler
);
410 ASSERT_EFI_ERROR (Status
);
412 Status
= gInterrupt
->RegisterInterruptSource (gInterrupt
, PcdGet32 (PcdArmArchTimerIntrNum
), TimerInterruptHandler
);
413 ASSERT_EFI_ERROR (Status
);
415 // Set up default timer
416 Status
= TimerDriverSetTimerPeriod (&gTimer
, FixedPcdGet32(PcdTimerPeriod
)); // TIMER_DEFAULT_PERIOD
417 ASSERT_EFI_ERROR (Status
);
419 // Install the Timer Architectural Protocol onto a new handle
420 Status
= gBS
->InstallMultipleProtocolInterfaces(
422 &gEfiTimerArchProtocolGuid
, &gTimer
,
425 ASSERT_EFI_ERROR(Status
);
427 // Everything is ready, unmask and enable timer interrupts
428 TimerCtrlReg
= ARM_ARCH_TIMER_ENABLE
;
429 ArmGenericTimerSetTimerCtrlReg (TimerCtrlReg
);
431 // Register for an ExitBootServicesEvent
432 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_NOTIFY
, ExitBootServicesEvent
, NULL
, &EfiExitBootServicesEvent
);
433 ASSERT_EFI_ERROR (Status
);