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 "XenTimerDxe.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;
50 @param InterruptType The type of interrupt that occurred
51 @param SystemContext A pointer to the system context when the interrupt occurred
55 TimerInterruptHandler (
56 IN EFI_EXCEPTION_TYPE InterruptType
,
57 IN EFI_SYSTEM_CONTEXT SystemContext
62 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
64 if (mTimerNotifyFunction
!= NULL
) {
66 // @bug : This does not handle missed timer interrupts
68 mTimerNotifyFunction (mTimerPeriod
);
71 gBS
->RestoreTPL (OriginalTPL
);
79 This function registers the handler NotifyFunction so it is called every time
80 the timer interrupt fires. It also passes the amount of time since the last
81 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
82 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
83 returned. If the CPU does not support registering a timer interrupt handler,
84 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
85 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
86 If an attempt is made to unregister a handler when a handler is not registered,
87 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
88 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
92 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
93 @param NotifyFunction The function to call when a timer interrupt fires. This
94 function executes at TPL_HIGH_LEVEL. The DXE Core will
95 register a handler for the timer interrupt, so it can know
96 how much time has passed. This information is used to
97 signal timer based events. NULL will unregister the handler.
99 @retval EFI_SUCCESS The timer handler was registered.
100 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
101 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
103 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
104 previously registered.
105 @retval EFI_DEVICE_ERROR The timer handler could not be registered.
110 TimerDriverRegisterHandler (
111 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
112 IN EFI_TIMER_NOTIFY NotifyFunction
116 // Check for invalid parameters
118 if (NotifyFunction
== NULL
&& mTimerNotifyFunction
== NULL
) {
119 return EFI_INVALID_PARAMETER
;
122 if (NotifyFunction
!= NULL
&& mTimerNotifyFunction
!= NULL
) {
123 return EFI_ALREADY_STARTED
;
126 mTimerNotifyFunction
= NotifyFunction
;
133 This function adjusts the period of timer interrupts to the value specified
134 by TimerPeriod. If the timer period is updated, then the selected timer
135 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
136 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
137 If an error occurs while attempting to update the timer period, then the
138 timer hardware will be put back in its state prior to this call, and
139 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
140 is disabled. This is not the same as disabling the CPU's interrupts.
141 Instead, it must either turn off the timer hardware, or it must adjust the
142 interrupt controller so that a CPU interrupt is not generated when the timer
146 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
147 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
148 the timer hardware is not programmable, then EFI_UNSUPPORTED is
149 returned. If the timer is programmable, then the timer period
150 will be rounded up to the nearest timer period that is supported
151 by the timer hardware. If TimerPeriod is set to 0, then the
152 timer interrupts will be disabled.
154 @retval EFI_SUCCESS The timer period was changed.
155 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
156 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
161 TimerDriverSetTimerPeriod (
162 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
163 IN UINT64 TimerPeriod
167 UINT32 TimerFrequency
;
168 UINT32 DivideValue
= 1;
170 if (TimerPeriod
== 0) {
172 // Disable timer interrupt for a TimerPeriod of 0
174 DisableApicTimerInterrupt();
176 TimerFrequency
= PcdGet32(PcdFSBClock
) / DivideValue
;
179 // Convert TimerPeriod into local APIC counts
181 // TimerPeriod is in 100ns
182 // TimerPeriod/10000000 will be in seconds.
183 TimerCount
= DivU64x32 (MultU64x32 (TimerPeriod
, TimerFrequency
),
186 // Check for overflow
187 if (TimerCount
> MAX_UINT32
) {
188 TimerCount
= MAX_UINT32
;
189 /* TimerPeriod = (MAX_UINT32 / TimerFrequency) * 10000000; */
190 TimerPeriod
= 429496730;
194 // Program the timer with the new count value
196 InitializeApicTimer(DivideValue
, (UINT32
)TimerCount
, TRUE
, LOCAL_APIC_TIMER_VECTOR
);
199 // Enable timer interrupt
201 EnableApicTimerInterrupt();
204 // Save the new timer period
206 mTimerPeriod
= TimerPeriod
;
213 This function retrieves the period of timer interrupts in 100 ns units,
214 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
215 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
216 returned, then the timer is currently disabled.
219 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
220 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
221 0 is returned, then the timer is currently disabled.
223 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
224 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
229 TimerDriverGetTimerPeriod (
230 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
231 OUT UINT64
*TimerPeriod
234 if (TimerPeriod
== NULL
) {
235 return EFI_INVALID_PARAMETER
;
238 *TimerPeriod
= mTimerPeriod
;
245 This function generates a soft timer interrupt. If the platform does not support soft
246 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
247 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
248 service, then a soft timer interrupt will be generated. If the timer interrupt is
249 enabled when this service is called, then the registered handler will be invoked. The
250 registered handler should not be able to distinguish a hardware-generated timer
251 interrupt from a software-generated timer interrupt.
254 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
256 @retval EFI_SUCCESS The soft timer interrupt was generated.
257 @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
262 TimerDriverGenerateSoftInterrupt (
263 IN EFI_TIMER_ARCH_PROTOCOL
*This
268 if (GetApicTimerInterruptState()) {
270 // Invoke the registered handler
272 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
274 if (mTimerNotifyFunction
!= NULL
) {
276 // @bug : This does not handle missed timer interrupts
278 mTimerNotifyFunction (mTimerPeriod
);
281 gBS
->RestoreTPL (OriginalTPL
);
283 return EFI_UNSUPPORTED
;
290 Initialize the Timer Architectural Protocol driver
292 @param ImageHandle ImageHandle of the loaded driver
293 @param SystemTable Pointer to the System Table
295 @retval EFI_SUCCESS Timer Architectural Protocol created
296 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
297 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
302 TimerDriverInitialize (
303 IN EFI_HANDLE ImageHandle
,
304 IN EFI_SYSTEM_TABLE
*SystemTable
310 // Initialize the pointer to our notify function.
312 mTimerNotifyFunction
= NULL
;
315 // Make sure the Timer Architectural Protocol is not already installed in the system
317 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiTimerArchProtocolGuid
);
320 // Find the CPU architectural protocol.
322 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**) &mCpu
);
323 ASSERT_EFI_ERROR (Status
);
326 // Force the timer to be disabled
328 Status
= TimerDriverSetTimerPeriod (&mTimer
, 0);
329 ASSERT_EFI_ERROR (Status
);
332 // Install interrupt handler for Local APIC Timer
334 Status
= mCpu
->RegisterInterruptHandler (mCpu
, LOCAL_APIC_TIMER_VECTOR
,
335 TimerInterruptHandler
);
336 ASSERT_EFI_ERROR (Status
);
339 // Force the timer to be enabled at its default period
341 Status
= TimerDriverSetTimerPeriod (&mTimer
, DEFAULT_TIMER_TICK_DURATION
);
342 ASSERT_EFI_ERROR (Status
);
345 // Install the Timer Architectural Protocol onto a new handle
347 Status
= gBS
->InstallMultipleProtocolInterfaces (
349 &gEfiTimerArchProtocolGuid
, &mTimer
,
352 ASSERT_EFI_ERROR (Status
);