2 Timer Architectural Protocol as defined in the DXE CIS
4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 // The handle onto which the Timer Architectural Protocol will be installed
14 EFI_HANDLE mTimerHandle
= NULL
;
17 // The Timer Architectural Protocol that this driver produces
19 EFI_TIMER_ARCH_PROTOCOL mTimer
= {
20 TimerDriverRegisterHandler
,
21 TimerDriverSetTimerPeriod
,
22 TimerDriverGetTimerPeriod
,
23 TimerDriverGenerateSoftInterrupt
27 // Pointer to the CPU Architectural Protocol instance
29 EFI_CPU_ARCH_PROTOCOL
*mCpu
;
32 // Pointer to the Legacy 8259 Protocol instance
34 EFI_LEGACY_8259_PROTOCOL
*mLegacy8259
;
37 // The notification function to call on every timer interrupt.
38 // A bug in the compiler prevents us from initializing this here.
40 EFI_TIMER_NOTIFY mTimerNotifyFunction
;
43 // The current period of the timer interrupt
45 volatile UINT64 mTimerPeriod
= 0;
52 Sets the counter value for Timer #0 in a legacy 8254 timer.
54 @param Count The 16-bit counter value to program into Timer #0 of the legacy 8254 timer.
61 IoWrite8 (TIMER_CONTROL_PORT
, 0x36);
62 IoWrite8 (TIMER0_COUNT_PORT
, (UINT8
)(Count
& 0xff));
63 IoWrite8 (TIMER0_COUNT_PORT
, (UINT8
)((Count
>> 8) & 0xff));
67 8254 Timer #0 Interrupt Handler.
69 @param InterruptType The type of interrupt that occurred
70 @param SystemContext A pointer to the system context when the interrupt occurred
74 TimerInterruptHandler (
75 IN EFI_EXCEPTION_TYPE InterruptType
,
76 IN EFI_SYSTEM_CONTEXT SystemContext
81 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
83 if (mTimerNotifyFunction
!= NULL
) {
85 // @bug : This does not handle missed timer interrupts
87 mTimerNotifyFunction (mTimerPeriod
);
90 gBS
->RestoreTPL (OriginalTPL
);
93 mLegacy8259
->EndOfInterrupt (mLegacy8259
, Efi8259Irq0
);
98 This function registers the handler NotifyFunction so it is called every time
99 the timer interrupt fires. It also passes the amount of time since the last
100 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
101 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
102 returned. If the CPU does not support registering a timer interrupt handler,
103 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
104 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
105 If an attempt is made to unregister a handler when a handler is not registered,
106 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
107 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
111 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
112 @param NotifyFunction The function to call when a timer interrupt fires. This
113 function executes at TPL_HIGH_LEVEL. The DXE Core will
114 register a handler for the timer interrupt, so it can know
115 how much time has passed. This information is used to
116 signal timer based events. NULL will unregister the handler.
118 @retval EFI_SUCCESS The timer handler was registered.
119 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
120 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
122 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
123 previously registered.
124 @retval EFI_DEVICE_ERROR The timer handler could not be registered.
129 TimerDriverRegisterHandler (
130 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
131 IN EFI_TIMER_NOTIFY NotifyFunction
135 // Check for invalid parameters
137 if ((NotifyFunction
== NULL
) && (mTimerNotifyFunction
== NULL
)) {
138 return EFI_INVALID_PARAMETER
;
141 if ((NotifyFunction
!= NULL
) && (mTimerNotifyFunction
!= NULL
)) {
142 return EFI_ALREADY_STARTED
;
145 mTimerNotifyFunction
= NotifyFunction
;
152 This function adjusts the period of timer interrupts to the value specified
153 by TimerPeriod. If the timer period is updated, then the selected timer
154 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
155 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
156 If an error occurs while attempting to update the timer period, then the
157 timer hardware will be put back in its state prior to this call, and
158 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
159 is disabled. This is not the same as disabling the CPU's interrupts.
160 Instead, it must either turn off the timer hardware, or it must adjust the
161 interrupt controller so that a CPU interrupt is not generated when the timer
165 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
166 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
167 the timer hardware is not programmable, then EFI_UNSUPPORTED is
168 returned. If the timer is programmable, then the timer period
169 will be rounded up to the nearest timer period that is supported
170 by the timer hardware. If TimerPeriod is set to 0, then the
171 timer interrupts will be disabled.
173 @retval EFI_SUCCESS The timer period was changed.
174 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
175 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
180 TimerDriverSetTimerPeriod (
181 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
182 IN UINT64 TimerPeriod
188 // The basic clock is 1.19318 MHz or 0.119318 ticks per 100 ns.
189 // TimerPeriod * 0.119318 = 8254 timer divisor. Using integer arithmetic
190 // TimerCount = (TimerPeriod * 119318)/1000000.
192 // Round up to next highest integer. This guarantees that the timer is
193 // equal to or slightly longer than the requested time.
194 // TimerCount = ((TimerPeriod * 119318) + 500000)/1000000
196 // Note that a TimerCount of 0 is equivalent to a count of 65,536
198 // Since TimerCount is limited to 16 bits for IA32, TimerPeriod is limited
201 if (TimerPeriod
== 0) {
203 // Disable timer interrupt for a TimerPeriod of 0
205 mLegacy8259
->DisableIrq (mLegacy8259
, Efi8259Irq0
);
208 // Convert TimerPeriod into 8254 counts
210 TimerCount
= DivU64x32 (MultU64x32 (119318, (UINT32
)TimerPeriod
) + 500000, 1000000);
213 // Check for overflow
215 if (TimerCount
>= 65536) {
217 TimerPeriod
= MAX_TIMER_TICK_DURATION
;
221 // Program the 8254 timer with the new count value
223 SetPitCount ((UINT16
)TimerCount
);
226 // Enable timer interrupt
228 mLegacy8259
->EnableIrq (mLegacy8259
, Efi8259Irq0
, FALSE
);
232 // Save the new timer period
234 mTimerPeriod
= TimerPeriod
;
241 This function retrieves the period of timer interrupts in 100 ns units,
242 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
243 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
244 returned, then the timer is currently disabled.
247 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
248 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
249 0 is returned, then the timer is currently disabled.
251 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
252 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
257 TimerDriverGetTimerPeriod (
258 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
259 OUT UINT64
*TimerPeriod
262 if (TimerPeriod
== NULL
) {
263 return EFI_INVALID_PARAMETER
;
266 *TimerPeriod
= mTimerPeriod
;
273 This function generates a soft timer interrupt. If the platform does not support soft
274 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
275 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
276 service, then a soft timer interrupt will be generated. If the timer interrupt is
277 enabled when this service is called, then the registered handler will be invoked. The
278 registered handler should not be able to distinguish a hardware-generated timer
279 interrupt from a software-generated timer interrupt.
282 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
284 @retval EFI_SUCCESS The soft timer interrupt was generated.
285 @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
290 TimerDriverGenerateSoftInterrupt (
291 IN EFI_TIMER_ARCH_PROTOCOL
*This
299 // If the timer interrupt is enabled, then the registered handler will be invoked.
301 Status
= mLegacy8259
->GetMask (mLegacy8259
, NULL
, NULL
, &IRQMask
, NULL
);
302 ASSERT_EFI_ERROR (Status
);
303 if ((IRQMask
& 0x1) == 0) {
305 // Invoke the registered handler
307 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
309 if (mTimerNotifyFunction
!= NULL
) {
311 // @bug : This does not handle missed timer interrupts
313 mTimerNotifyFunction (mTimerPeriod
);
316 gBS
->RestoreTPL (OriginalTPL
);
318 return EFI_UNSUPPORTED
;
325 Initialize the Timer Architectural Protocol driver
327 @param ImageHandle ImageHandle of the loaded driver
328 @param SystemTable Pointer to the System Table
330 @retval EFI_SUCCESS Timer Architectural Protocol created
331 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
332 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
337 TimerDriverInitialize (
338 IN EFI_HANDLE ImageHandle
,
339 IN EFI_SYSTEM_TABLE
*SystemTable
346 // Initialize the pointer to our notify function.
348 mTimerNotifyFunction
= NULL
;
351 // Make sure the Timer Architectural Protocol is not already installed in the system
353 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiTimerArchProtocolGuid
);
356 // Find the CPU architectural protocol.
358 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&mCpu
);
359 ASSERT_EFI_ERROR (Status
);
362 // Find the Legacy8259 protocol.
364 Status
= gBS
->LocateProtocol (&gEfiLegacy8259ProtocolGuid
, NULL
, (VOID
**)&mLegacy8259
);
365 ASSERT_EFI_ERROR (Status
);
368 // Force the timer to be disabled
370 Status
= TimerDriverSetTimerPeriod (&mTimer
, 0);
371 ASSERT_EFI_ERROR (Status
);
374 // Get the interrupt vector number corresponding to IRQ0 from the 8259 driver
377 Status
= mLegacy8259
->GetVector (mLegacy8259
, Efi8259Irq0
, (UINT8
*)&TimerVector
);
378 ASSERT_EFI_ERROR (Status
);
381 // Install interrupt handler for 8254 Timer #0 (ISA IRQ0)
383 Status
= mCpu
->RegisterInterruptHandler (mCpu
, TimerVector
, TimerInterruptHandler
);
384 ASSERT_EFI_ERROR (Status
);
387 // Force the timer to be enabled at its default period
389 Status
= TimerDriverSetTimerPeriod (&mTimer
, DEFAULT_TIMER_TICK_DURATION
);
390 ASSERT_EFI_ERROR (Status
);
393 // Install the Timer Architectural Protocol onto a new handle
395 Status
= gBS
->InstallMultipleProtocolInterfaces (
397 &gEfiTimerArchProtocolGuid
,
401 ASSERT_EFI_ERROR (Status
);