2 Timer Architectural Protocol as defined in the DXE CIS
4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2019, Citrix Systems, Inc.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "LocalApicTimerDxe.h"
14 // The handle onto which the Timer Architectural Protocol will be installed
16 EFI_HANDLE mTimerHandle
= NULL
;
19 // The Timer Architectural Protocol that this driver produces
21 EFI_TIMER_ARCH_PROTOCOL mTimer
= {
22 TimerDriverRegisterHandler
,
23 TimerDriverSetTimerPeriod
,
24 TimerDriverGetTimerPeriod
,
25 TimerDriverGenerateSoftInterrupt
29 // Pointer to the CPU Architectural Protocol instance
31 EFI_CPU_ARCH_PROTOCOL
*mCpu
;
34 // The notification function to call on every timer interrupt.
35 // A bug in the compiler prevents us from initializing this here.
37 EFI_TIMER_NOTIFY mTimerNotifyFunction
;
40 // The current period of the timer interrupt
42 volatile UINT64 mTimerPeriod
= 0;
51 @param InterruptType The type of interrupt that occurred
52 @param SystemContext A pointer to the system context when the interrupt occurred
56 TimerInterruptHandler (
57 IN EFI_EXCEPTION_TYPE InterruptType
,
58 IN EFI_SYSTEM_CONTEXT SystemContext
63 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
65 if (mTimerNotifyFunction
!= NULL
) {
67 // @bug : This does not handle missed timer interrupts
69 mTimerNotifyFunction (mTimerPeriod
);
72 gBS
->RestoreTPL (OriginalTPL
);
80 This function registers the handler NotifyFunction so it is called every time
81 the timer interrupt fires. It also passes the amount of time since the last
82 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
83 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
84 returned. If the CPU does not support registering a timer interrupt handler,
85 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
86 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
87 If an attempt is made to unregister a handler when a handler is not registered,
88 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
89 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
93 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
94 @param NotifyFunction The function to call when a timer interrupt fires. This
95 function executes at TPL_HIGH_LEVEL. The DXE Core will
96 register a handler for the timer interrupt, so it can know
97 how much time has passed. This information is used to
98 signal timer based events. NULL will unregister the handler.
100 @retval EFI_SUCCESS The timer handler was registered.
101 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
102 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
104 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
105 previously registered.
106 @retval EFI_DEVICE_ERROR The timer handler could not be registered.
111 TimerDriverRegisterHandler (
112 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
113 IN EFI_TIMER_NOTIFY NotifyFunction
117 // Check for invalid parameters
119 if ((NotifyFunction
== NULL
) && (mTimerNotifyFunction
== NULL
)) {
120 return EFI_INVALID_PARAMETER
;
123 if ((NotifyFunction
!= NULL
) && (mTimerNotifyFunction
!= NULL
)) {
124 return EFI_ALREADY_STARTED
;
127 mTimerNotifyFunction
= NotifyFunction
;
134 This function adjusts the period of timer interrupts to the value specified
135 by TimerPeriod. If the timer period is updated, then the selected timer
136 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
137 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
138 If an error occurs while attempting to update the timer period, then the
139 timer hardware will be put back in its state prior to this call, and
140 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
141 is disabled. This is not the same as disabling the CPU's interrupts.
142 Instead, it must either turn off the timer hardware, or it must adjust the
143 interrupt controller so that a CPU interrupt is not generated when the timer
147 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
148 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
149 the timer hardware is not programmable, then EFI_UNSUPPORTED is
150 returned. If the timer is programmable, then the timer period
151 will be rounded up to the nearest timer period that is supported
152 by the timer hardware. If TimerPeriod is set to 0, then the
153 timer interrupts will be disabled.
155 @retval EFI_SUCCESS The timer period was changed.
156 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
157 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
162 TimerDriverSetTimerPeriod (
163 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
164 IN UINT64 TimerPeriod
168 UINT32 TimerFrequency
;
169 UINT32 DivideValue
= 1;
171 if (TimerPeriod
== 0) {
173 // Disable timer interrupt for a TimerPeriod of 0
175 DisableApicTimerInterrupt ();
177 TimerFrequency
= PcdGet32 (PcdFSBClock
) / (UINT32
)DivideValue
;
180 // Convert TimerPeriod into local APIC counts
182 // TimerPeriod is in 100ns
183 // TimerPeriod/10000000 will be in seconds.
184 TimerCount
= DivU64x32 (
185 MultU64x32 (TimerPeriod
, TimerFrequency
),
189 // Check for overflow
190 if (TimerCount
> MAX_UINT32
) {
191 TimerCount
= MAX_UINT32
;
192 /* TimerPeriod = (MAX_UINT32 / TimerFrequency) * 10000000; */
193 TimerPeriod
= 429496730;
197 // Program the timer with the new count value
199 InitializeApicTimer (DivideValue
, (UINT32
)TimerCount
, TRUE
, LOCAL_APIC_TIMER_VECTOR
);
202 // Enable timer interrupt
204 EnableApicTimerInterrupt ();
208 // Save the new timer period
210 mTimerPeriod
= TimerPeriod
;
217 This function retrieves the period of timer interrupts in 100 ns units,
218 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
219 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
220 returned, then the timer is currently disabled.
223 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
224 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
225 0 is returned, then the timer is currently disabled.
227 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
228 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
233 TimerDriverGetTimerPeriod (
234 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
235 OUT UINT64
*TimerPeriod
238 if (TimerPeriod
== NULL
) {
239 return EFI_INVALID_PARAMETER
;
242 *TimerPeriod
= mTimerPeriod
;
249 This function generates a soft timer interrupt. If the platform does not support soft
250 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
251 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
252 service, then a soft timer interrupt will be generated. If the timer interrupt is
253 enabled when this service is called, then the registered handler will be invoked. The
254 registered handler should not be able to distinguish a hardware-generated timer
255 interrupt from a software-generated timer interrupt.
258 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
260 @retval EFI_SUCCESS The soft timer interrupt was generated.
261 @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
266 TimerDriverGenerateSoftInterrupt (
267 IN EFI_TIMER_ARCH_PROTOCOL
*This
272 if (GetApicTimerInterruptState ()) {
274 // Invoke the registered handler
276 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
278 if (mTimerNotifyFunction
!= NULL
) {
280 // @bug : This does not handle missed timer interrupts
282 mTimerNotifyFunction (mTimerPeriod
);
285 gBS
->RestoreTPL (OriginalTPL
);
287 return EFI_UNSUPPORTED
;
294 Initialize the Timer Architectural Protocol driver
296 @param ImageHandle ImageHandle of the loaded driver
297 @param SystemTable Pointer to the System Table
299 @retval EFI_SUCCESS Timer Architectural Protocol created
300 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
301 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
306 TimerDriverInitialize (
307 IN EFI_HANDLE ImageHandle
,
308 IN EFI_SYSTEM_TABLE
*SystemTable
314 // Initialize the pointer to our notify function.
316 mTimerNotifyFunction
= NULL
;
319 // Make sure the Timer Architectural Protocol is not already installed in the system
321 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiTimerArchProtocolGuid
);
324 // Find the CPU architectural protocol.
326 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&mCpu
);
327 ASSERT_EFI_ERROR (Status
);
330 // Force the timer to be disabled
332 Status
= TimerDriverSetTimerPeriod (&mTimer
, 0);
333 ASSERT_EFI_ERROR (Status
);
336 // Install interrupt handler for Local APIC Timer
338 Status
= mCpu
->RegisterInterruptHandler (
340 LOCAL_APIC_TIMER_VECTOR
,
341 TimerInterruptHandler
343 ASSERT_EFI_ERROR (Status
);
346 // Force the timer to be enabled at its default period
348 Status
= TimerDriverSetTimerPeriod (&mTimer
, DEFAULT_TIMER_TICK_DURATION
);
349 ASSERT_EFI_ERROR (Status
);
352 // Install the Timer Architectural Protocol onto a new handle
354 Status
= gBS
->InstallMultipleProtocolInterfaces (
356 &gEfiTimerArchProtocolGuid
,
360 ASSERT_EFI_ERROR (Status
);