2 Timer Architectural Protocol module using High Precision Event Timer (HPET)
4 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Protocol/Cpu.h>
12 #include <Protocol/Timer.h>
14 #include <Library/IoLib.h>
15 #include <Library/PcdLib.h>
16 #include <Library/BaseLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/LocalApicLib.h>
20 #include <Library/IoApicLib.h>
22 #include <Register/LocalApic.h>
23 #include <Register/IoApic.h>
24 #include <Register/Hpet.h>
27 /// Define value for an invalid HPET Timer index.
29 #define HPET_INVALID_TIMER_INDEX 0xff
32 /// Timer Architectural Protocol function prototypes.
36 This function registers the handler NotifyFunction so it is called every time
37 the timer interrupt fires. It also passes the amount of time since the last
38 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
39 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
40 returned. If the CPU does not support registering a timer interrupt handler,
41 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
42 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
43 If an attempt is made to unregister a handler when a handler is not registered,
44 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
45 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
48 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
49 @param NotifyFunction The function to call when a timer interrupt fires.
50 This function executes at TPL_HIGH_LEVEL. The DXE
51 Core will register a handler for the timer interrupt,
52 so it can know how much time has passed. This
53 information is used to signal timer based events.
54 NULL will unregister the handler.
56 @retval EFI_SUCCESS The timer handler was registered.
57 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
58 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
60 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
61 previously registered.
62 @retval EFI_DEVICE_ERROR The timer handler could not be registered.
67 TimerDriverRegisterHandler (
68 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
69 IN EFI_TIMER_NOTIFY NotifyFunction
73 This function adjusts the period of timer interrupts to the value specified
74 by TimerPeriod. If the timer period is updated, then the selected timer
75 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
76 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
77 If an error occurs while attempting to update the timer period, then the
78 timer hardware will be put back in its state prior to this call, and
79 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
80 is disabled. This is not the same as disabling the CPU's interrupts.
81 Instead, it must either turn off the timer hardware, or it must adjust the
82 interrupt controller so that a CPU interrupt is not generated when the timer
85 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
86 @param TimerPeriod The rate to program the timer interrupt in 100 nS units.
87 If the timer hardware is not programmable, then
88 EFI_UNSUPPORTED is returned. If the timer is programmable,
89 then the timer period will be rounded up to the nearest
90 timer period that is supported by the timer hardware.
91 If TimerPeriod is set to 0, then the timer interrupts
94 @retval EFI_SUCCESS The timer period was changed.
95 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
96 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
101 TimerDriverSetTimerPeriod (
102 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
103 IN UINT64 TimerPeriod
107 This function retrieves the period of timer interrupts in 100 ns units,
108 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
109 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
110 returned, then the timer is currently disabled.
112 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
113 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units.
114 If 0 is returned, then the timer is currently disabled.
116 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
117 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
122 TimerDriverGetTimerPeriod (
123 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
124 OUT UINT64
*TimerPeriod
128 This function generates a soft timer interrupt. If the platform does not support soft
129 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
130 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
131 service, then a soft timer interrupt will be generated. If the timer interrupt is
132 enabled when this service is called, then the registered handler will be invoked. The
133 registered handler should not be able to distinguish a hardware-generated timer
134 interrupt from a software-generated timer interrupt.
136 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
138 @retval EFI_SUCCESS The soft timer interrupt was generated.
139 @retval EFI_UNSUPPORTED The platform does not support the generation of soft
145 TimerDriverGenerateSoftInterrupt (
146 IN EFI_TIMER_ARCH_PROTOCOL
*This
150 /// The handle onto which the Timer Architectural Protocol will be installed.
152 EFI_HANDLE mTimerHandle
= NULL
;
155 /// The Timer Architectural Protocol that this driver produces.
157 EFI_TIMER_ARCH_PROTOCOL mTimer
= {
158 TimerDriverRegisterHandler
,
159 TimerDriverSetTimerPeriod
,
160 TimerDriverGetTimerPeriod
,
161 TimerDriverGenerateSoftInterrupt
165 /// Pointer to the CPU Architectural Protocol instance.
167 EFI_CPU_ARCH_PROTOCOL
*mCpu
= NULL
;
170 /// The notification function to call on every timer interrupt.
172 EFI_TIMER_NOTIFY mTimerNotifyFunction
= NULL
;
175 /// The current period of the HPET timer interrupt in 100 ns units.
177 UINT64 mTimerPeriod
= 0;
180 /// The number of HPET timer ticks required for the current HPET rate specified by mTimerPeriod.
185 /// Mask used for counter and comparator calculations to adjust for a 32-bit or 64-bit counter.
190 /// The HPET main counter value from the most recent HPET timer interrupt.
192 volatile UINT64 mPreviousMainCounter
;
194 volatile UINT64 mPreviousComparator
;
197 /// The index of the HPET timer being managed by this driver.
202 /// The I/O APIC IRQ that the HPET Timer is mapped if I/O APIC mode is used.
207 /// Cached state of the HPET General Capabilities register managed by this driver.
208 /// Caching the state reduces the number of times the configuration register is read.
210 HPET_GENERAL_CAPABILITIES_ID_REGISTER mHpetGeneralCapabilities
;
213 /// Cached state of the HPET General Configuration register managed by this driver.
214 /// Caching the state reduces the number of times the configuration register is read.
216 HPET_GENERAL_CONFIGURATION_REGISTER mHpetGeneralConfiguration
;
219 /// Cached state of the Configuration register for the HPET Timer managed by
220 /// this driver. Caching the state reduces the number of times the configuration
221 /// register is read.
223 HPET_TIMER_CONFIGURATION_REGISTER mTimerConfiguration
;
226 /// Counts the number of HPET Timer interrupts processed by this driver.
227 /// Only required for debug.
229 volatile UINTN mNumTicks
;
232 Read a 64-bit register from the HPET
234 @param Offset Specifies the offset of the HPET register to read.
236 @return The 64-bit value read from the HPET register specified by Offset.
243 return MmioRead64 (PcdGet32 (PcdHpetBaseAddress
) + Offset
);
247 Write a 64-bit HPET register.
249 @param Offset Specifies the offset of the HPET register to write.
250 @param Value Specifies the value to write to the HPET register specified by Offset.
252 @return The 64-bit value written to HPET register specified by Offset.
260 return MmioWrite64 (PcdGet32 (PcdHpetBaseAddress
) + Offset
, Value
);
264 Enable or disable the main counter in the HPET Timer.
266 @param Enable If TRUE, then enable the main counter in the HPET Timer.
267 If FALSE, then disable the main counter in the HPET Timer.
274 mHpetGeneralConfiguration
.Bits
.MainCounterEnable
= Enable
? 1 : 0;
275 HpetWrite (HPET_GENERAL_CONFIGURATION_OFFSET
, mHpetGeneralConfiguration
.Uint64
);
279 The interrupt handler for the HPET timer. This handler clears the HPET interrupt
280 and computes the amount of time that has passed since the last HPET timer interrupt.
281 If a notification function is registered, then the amount of time since the last
282 HPET interrupt is passed to that notification function in 100 ns units. The HPET
283 time is updated to generate another interrupt in the required time period.
285 @param InterruptType The type of interrupt that occurred.
286 @param SystemContext A pointer to the system context when the interrupt occurred.
290 TimerInterruptHandler (
291 IN EFI_EXCEPTION_TYPE InterruptType
,
292 IN EFI_SYSTEM_CONTEXT SystemContext
301 // Count number of ticks
303 DEBUG_CODE (mNumTicks
++;);
306 // Clear HPET timer interrupt status
308 HpetWrite (HPET_GENERAL_INTERRUPT_STATUS_OFFSET
, LShiftU64 (1, mTimerIndex
));
316 // Disable HPET timer when adjusting the COMPARATOR value to prevent a missed interrupt
321 // Capture main counter value
323 MainCounter
= HpetRead (HPET_MAIN_COUNTER_OFFSET
);
326 // Get the previous comparator counter
328 mPreviousComparator
= HpetRead (HPET_TIMER_COMPARATOR_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
);
331 // Set HPET COMPARATOR to the value required for the next timer tick
333 Comparator
= (mPreviousComparator
+ mTimerCount
) & mCounterMask
;
335 if ((mPreviousMainCounter
< MainCounter
) && (mPreviousComparator
> Comparator
)) {
337 // When comparator overflows
339 HpetWrite (HPET_TIMER_COMPARATOR_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, Comparator
);
340 } else if ((mPreviousMainCounter
> MainCounter
) && (mPreviousComparator
< Comparator
)) {
342 // When main counter overflows
344 HpetWrite (HPET_TIMER_COMPARATOR_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, (MainCounter
+ mTimerCount
) & mCounterMask
);
347 // When both main counter and comparator do not overflow or both do overflow
349 if (Comparator
> MainCounter
) {
350 HpetWrite (HPET_TIMER_COMPARATOR_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, Comparator
);
352 HpetWrite (HPET_TIMER_COMPARATOR_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, (MainCounter
+ mTimerCount
) & mCounterMask
);
357 // Enable the HPET counter once the new COMPARATOR value has been set.
362 // Check to see if there is a registered notification function
364 if (mTimerNotifyFunction
!= NULL
) {
366 // Compute time since last notification in 100 ns units (10 ^ -7)
368 if (MainCounter
> mPreviousMainCounter
) {
370 // Main counter does not overflow
372 Delta
= MainCounter
- mPreviousMainCounter
;
375 // Main counter overflows, first usb, then add
377 Delta
= (mCounterMask
- mPreviousMainCounter
) + MainCounter
;
379 TimerPeriod
= DivU64x32 (
381 Delta
& mCounterMask
,
382 mHpetGeneralCapabilities
.Bits
.CounterClockPeriod
388 // Call registered notification function passing in the time since the last
389 // interrupt in 100 ns units.
391 mTimerNotifyFunction (TimerPeriod
);
395 // Save main counter value
397 mPreviousMainCounter
= MainCounter
;
401 This function registers the handler NotifyFunction so it is called every time
402 the timer interrupt fires. It also passes the amount of time since the last
403 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
404 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
405 returned. If the CPU does not support registering a timer interrupt handler,
406 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
407 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
408 If an attempt is made to unregister a handler when a handler is not registered,
409 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
410 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
413 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
414 @param NotifyFunction The function to call when a timer interrupt fires.
415 This function executes at TPL_HIGH_LEVEL. The DXE
416 Core will register a handler for the timer interrupt,
417 so it can know how much time has passed. This
418 information is used to signal timer based events.
419 NULL will unregister the handler.
421 @retval EFI_SUCCESS The timer handler was registered.
422 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
423 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
425 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
426 previously registered.
427 @retval EFI_DEVICE_ERROR The timer handler could not be registered.
432 TimerDriverRegisterHandler (
433 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
434 IN EFI_TIMER_NOTIFY NotifyFunction
438 // Check for invalid parameters
440 if (NotifyFunction
== NULL
&& mTimerNotifyFunction
== NULL
) {
441 return EFI_INVALID_PARAMETER
;
443 if (NotifyFunction
!= NULL
&& mTimerNotifyFunction
!= NULL
) {
444 return EFI_ALREADY_STARTED
;
448 // Cache the registered notification function
450 mTimerNotifyFunction
= NotifyFunction
;
456 This function adjusts the period of timer interrupts to the value specified
457 by TimerPeriod. If the timer period is updated, then the selected timer
458 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
459 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
460 If an error occurs while attempting to update the timer period, then the
461 timer hardware will be put back in its state prior to this call, and
462 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
463 is disabled. This is not the same as disabling the CPU's interrupts.
464 Instead, it must either turn off the timer hardware, or it must adjust the
465 interrupt controller so that a CPU interrupt is not generated when the timer
468 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
469 @param TimerPeriod The rate to program the timer interrupt in 100 nS units.
470 If the timer hardware is not programmable, then
471 EFI_UNSUPPORTED is returned. If the timer is programmable,
472 then the timer period will be rounded up to the nearest
473 timer period that is supported by the timer hardware.
474 If TimerPeriod is set to 0, then the timer interrupts
477 @retval EFI_SUCCESS The timer period was changed.
478 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
479 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
484 TimerDriverSetTimerPeriod (
485 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
486 IN UINT64 TimerPeriod
492 UINT64 CurrentComparator
;
493 HPET_TIMER_MSI_ROUTE_REGISTER HpetTimerMsiRoute
;
496 // Disable interrupts
498 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
501 // Disable HPET timer when adjusting the timer period
505 if (TimerPeriod
== 0) {
506 if (mTimerPeriod
!= 0) {
508 // Check if there is possibly a pending interrupt
510 MainCounter
= HpetRead (HPET_MAIN_COUNTER_OFFSET
);
511 if (MainCounter
< mPreviousMainCounter
) {
512 Delta
= (mCounterMask
- mPreviousMainCounter
) + MainCounter
;
514 Delta
= MainCounter
- mPreviousMainCounter
;
516 if ((Delta
& mCounterMask
) >= mTimerCount
) {
518 // Interrupt still happens after disable HPET, wait to be processed
519 // Wait until interrupt is processed and comparator is increased
521 CurrentComparator
= HpetRead (HPET_TIMER_COMPARATOR_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
);
522 while (CurrentComparator
== mPreviousComparator
) {
523 CurrentComparator
= HpetRead (HPET_TIMER_COMPARATOR_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
);
530 // If TimerPeriod is 0, then mask HPET Timer interrupts
533 if (mTimerConfiguration
.Bits
.MsiInterruptCapability
!= 0 && FeaturePcdGet (PcdHpetMsiEnable
)) {
535 // Disable HPET MSI interrupt generation
537 mTimerConfiguration
.Bits
.MsiInterruptEnable
= 0;
540 // Disable I/O APIC Interrupt
542 IoApicEnableInterrupt (mTimerIrq
, FALSE
);
546 // Disable HPET timer interrupt
548 mTimerConfiguration
.Bits
.InterruptEnable
= 0;
549 HpetWrite (HPET_TIMER_CONFIGURATION_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, mTimerConfiguration
.Uint64
);
552 // Convert TimerPeriod to femtoseconds and divide by the number if femtoseconds
553 // per tick of the HPET counter to determine the number of HPET counter ticks
554 // in TimerPeriod 100 ns units.
556 mTimerCount
= DivU64x32 (
557 MultU64x32 (TimerPeriod
, 100000000),
558 mHpetGeneralCapabilities
.Bits
.CounterClockPeriod
562 // Program the HPET Comparator with the number of ticks till the next interrupt
564 MainCounter
= HpetRead (HPET_MAIN_COUNTER_OFFSET
);
565 if (MainCounter
> mPreviousMainCounter
) {
566 Delta
= MainCounter
- mPreviousMainCounter
;
568 Delta
= (mCounterMask
- mPreviousMainCounter
) + MainCounter
;
570 if ((Delta
& mCounterMask
) >= mTimerCount
) {
571 HpetWrite (HPET_TIMER_COMPARATOR_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, (MainCounter
+ 1) & mCounterMask
);
573 HpetWrite (HPET_TIMER_COMPARATOR_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, (mPreviousMainCounter
+ mTimerCount
) & mCounterMask
);
577 // Enable HPET Timer interrupt generation
579 if (mTimerConfiguration
.Bits
.MsiInterruptCapability
!= 0 && FeaturePcdGet (PcdHpetMsiEnable
)) {
581 // Program MSI Address and MSI Data values in the selected HPET Timer
582 // Program HPET register with APIC ID of current BSP in case BSP has been switched
584 HpetTimerMsiRoute
.Bits
.Address
= GetApicMsiAddress ();
585 HpetTimerMsiRoute
.Bits
.Value
= (UINT32
)GetApicMsiValue (PcdGet8 (PcdHpetLocalApicVector
), LOCAL_APIC_DELIVERY_MODE_LOWEST_PRIORITY
, FALSE
, FALSE
);
586 HpetWrite (HPET_TIMER_MSI_ROUTE_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, HpetTimerMsiRoute
.Uint64
);
588 // Enable HPET MSI Interrupt
590 mTimerConfiguration
.Bits
.MsiInterruptEnable
= 1;
593 // Enable timer interrupt through I/O APIC
594 // Program IOAPIC register with APIC ID of current BSP in case BSP has been switched
596 IoApicConfigureInterrupt (mTimerIrq
, PcdGet8 (PcdHpetLocalApicVector
), IO_APIC_DELIVERY_MODE_LOWEST_PRIORITY
, TRUE
, FALSE
);
597 IoApicEnableInterrupt (mTimerIrq
, TRUE
);
601 // Enable HPET Interrupt Generation
603 mTimerConfiguration
.Bits
.InterruptEnable
= 1;
604 HpetWrite (HPET_TIMER_CONFIGURATION_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, mTimerConfiguration
.Uint64
);
608 // Save the new timer period
610 mTimerPeriod
= TimerPeriod
;
613 // Enable the HPET counter once new timer period has been established
614 // The HPET counter should run even if the HPET Timer interrupts are
615 // disabled. This is used to account for time passed while the interrupt
621 // Restore interrupts
623 gBS
->RestoreTPL (Tpl
);
629 This function retrieves the period of timer interrupts in 100 ns units,
630 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
631 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
632 returned, then the timer is currently disabled.
634 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
635 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units.
636 If 0 is returned, then the timer is currently disabled.
638 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
639 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
644 TimerDriverGetTimerPeriod (
645 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
646 OUT UINT64
*TimerPeriod
649 if (TimerPeriod
== NULL
) {
650 return EFI_INVALID_PARAMETER
;
653 *TimerPeriod
= mTimerPeriod
;
659 This function generates a soft timer interrupt. If the platform does not support soft
660 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
661 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
662 service, then a soft timer interrupt will be generated. If the timer interrupt is
663 enabled when this service is called, then the registered handler will be invoked. The
664 registered handler should not be able to distinguish a hardware-generated timer
665 interrupt from a software-generated timer interrupt.
667 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
669 @retval EFI_SUCCESS The soft timer interrupt was generated.
670 @retval EFI_UNSUPPORTED The platform does not support the generation of soft
676 TimerDriverGenerateSoftInterrupt (
677 IN EFI_TIMER_ARCH_PROTOCOL
*This
686 // Disable interrupts
688 Tpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
691 // Capture main counter value
693 MainCounter
= HpetRead (HPET_MAIN_COUNTER_OFFSET
);
696 // Check to see if there is a registered notification function
698 if (mTimerNotifyFunction
!= NULL
) {
700 // Compute time since last interrupt in 100 ns units (10 ^ -7)
702 if (MainCounter
> mPreviousMainCounter
) {
704 // Main counter does not overflow
706 Delta
= MainCounter
- mPreviousMainCounter
;
709 // Main counter overflows, first usb, then add
711 Delta
= (mCounterMask
- mPreviousMainCounter
) + MainCounter
;
714 TimerPeriod
= DivU64x32 (
716 Delta
& mCounterMask
,
717 mHpetGeneralCapabilities
.Bits
.CounterClockPeriod
723 // Call registered notification function passing in the time since the last
724 // interrupt in 100 ns units.
726 mTimerNotifyFunction (TimerPeriod
);
730 // Save main counter value
732 mPreviousMainCounter
= MainCounter
;
735 // Restore interrupts
737 gBS
->RestoreTPL (Tpl
);
743 Initialize the Timer Architectural Protocol driver
745 @param ImageHandle ImageHandle of the loaded driver
746 @param SystemTable Pointer to the System Table
748 @retval EFI_SUCCESS Timer Architectural Protocol created
749 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
750 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
755 TimerDriverInitialize (
756 IN EFI_HANDLE ImageHandle
,
757 IN EFI_SYSTEM_TABLE
*SystemTable
763 HPET_TIMER_MSI_ROUTE_REGISTER HpetTimerMsiRoute
;
765 DEBUG ((DEBUG_INFO
, "Init HPET Timer Driver\n"));
768 // Make sure the Timer Architectural Protocol is not already installed in the system
770 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiTimerArchProtocolGuid
);
773 // Find the CPU architectural protocol.
775 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**) &mCpu
);
776 ASSERT_EFI_ERROR (Status
);
779 // Retrieve HPET Capabilities and Configuration Information
781 mHpetGeneralCapabilities
.Uint64
= HpetRead (HPET_GENERAL_CAPABILITIES_ID_OFFSET
);
782 mHpetGeneralConfiguration
.Uint64
= HpetRead (HPET_GENERAL_CONFIGURATION_OFFSET
);
785 // If Revision is not valid, then ASSERT() and unload the driver because the HPET
786 // device is not present.
788 ASSERT (mHpetGeneralCapabilities
.Uint64
!= 0);
789 ASSERT (mHpetGeneralCapabilities
.Uint64
!= 0xFFFFFFFFFFFFFFFFULL
);
790 if (mHpetGeneralCapabilities
.Uint64
== 0 || mHpetGeneralCapabilities
.Uint64
== 0xFFFFFFFFFFFFFFFFULL
) {
791 DEBUG ((DEBUG_ERROR
, "HPET device is not present. Unload HPET driver.\n"));
792 return EFI_DEVICE_ERROR
;
796 // Force the HPET timer to be disabled while setting everything up
801 // Dump HPET Configuration Information
804 DEBUG ((DEBUG_INFO
, "HPET Base Address = 0x%08x\n", PcdGet32 (PcdHpetBaseAddress
)));
805 DEBUG ((DEBUG_INFO
, " HPET_GENERAL_CAPABILITIES_ID = 0x%016lx\n", mHpetGeneralCapabilities
));
806 DEBUG ((DEBUG_INFO
, " HPET_GENERAL_CONFIGURATION = 0x%016lx\n", mHpetGeneralConfiguration
.Uint64
));
807 DEBUG ((DEBUG_INFO
, " HPET_GENERAL_INTERRUPT_STATUS = 0x%016lx\n", HpetRead (HPET_GENERAL_INTERRUPT_STATUS_OFFSET
)));
808 DEBUG ((DEBUG_INFO
, " HPET_MAIN_COUNTER = 0x%016lx\n", HpetRead (HPET_MAIN_COUNTER_OFFSET
)));
809 DEBUG ((DEBUG_INFO
, " HPET Main Counter Period = %d (fs)\n", mHpetGeneralCapabilities
.Bits
.CounterClockPeriod
));
810 for (TimerIndex
= 0; TimerIndex
<= mHpetGeneralCapabilities
.Bits
.NumberOfTimers
; TimerIndex
++) {
811 DEBUG ((DEBUG_INFO
, " HPET_TIMER%d_CONFIGURATION = 0x%016lx\n", TimerIndex
, HpetRead (HPET_TIMER_CONFIGURATION_OFFSET
+ TimerIndex
* HPET_TIMER_STRIDE
)));
812 DEBUG ((DEBUG_INFO
, " HPET_TIMER%d_COMPARATOR = 0x%016lx\n", TimerIndex
, HpetRead (HPET_TIMER_COMPARATOR_OFFSET
+ TimerIndex
* HPET_TIMER_STRIDE
)));
813 DEBUG ((DEBUG_INFO
, " HPET_TIMER%d_MSI_ROUTE = 0x%016lx\n", TimerIndex
, HpetRead (HPET_TIMER_MSI_ROUTE_OFFSET
+ TimerIndex
* HPET_TIMER_STRIDE
)));
818 // Capture the current HPET main counter value.
820 mPreviousMainCounter
= HpetRead (HPET_MAIN_COUNTER_OFFSET
);
823 // Determine the interrupt mode to use for the HPET Timer.
824 // Look for MSI first, then unused PIC mode interrupt, then I/O APIC mode interrupt
826 MsiTimerIndex
= HPET_INVALID_TIMER_INDEX
;
827 mTimerIndex
= HPET_INVALID_TIMER_INDEX
;
828 for (TimerIndex
= 0; TimerIndex
<= mHpetGeneralCapabilities
.Bits
.NumberOfTimers
; TimerIndex
++) {
830 // Read the HPET Timer Capabilities and Configuration register
832 mTimerConfiguration
.Uint64
= HpetRead (HPET_TIMER_CONFIGURATION_OFFSET
+ TimerIndex
* HPET_TIMER_STRIDE
);
835 // Check to see if this HPET Timer supports MSI
837 if (mTimerConfiguration
.Bits
.MsiInterruptCapability
!= 0) {
839 // Save the index of the first HPET Timer that supports MSI interrupts
841 if (MsiTimerIndex
== HPET_INVALID_TIMER_INDEX
) {
842 MsiTimerIndex
= TimerIndex
;
847 // Check to see if this HPET Timer supports I/O APIC interrupts
849 if (mTimerConfiguration
.Bits
.InterruptRouteCapability
!= 0) {
851 // Save the index of the first HPET Timer that supports I/O APIC interrupts
853 if (mTimerIndex
== HPET_INVALID_TIMER_INDEX
) {
854 mTimerIndex
= TimerIndex
;
855 mTimerIrq
= (UINT32
)LowBitSet32 (mTimerConfiguration
.Bits
.InterruptRouteCapability
);
860 if (FeaturePcdGet (PcdHpetMsiEnable
) && MsiTimerIndex
!= HPET_INVALID_TIMER_INDEX
) {
862 // Use MSI interrupt if supported
864 mTimerIndex
= MsiTimerIndex
;
867 // Program MSI Address and MSI Data values in the selected HPET Timer
869 HpetTimerMsiRoute
.Bits
.Address
= GetApicMsiAddress ();
870 HpetTimerMsiRoute
.Bits
.Value
= (UINT32
)GetApicMsiValue (PcdGet8 (PcdHpetLocalApicVector
), LOCAL_APIC_DELIVERY_MODE_LOWEST_PRIORITY
, FALSE
, FALSE
);
871 HpetWrite (HPET_TIMER_MSI_ROUTE_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, HpetTimerMsiRoute
.Uint64
);
874 // Read the HPET Timer Capabilities and Configuration register and initialize for MSI mode
875 // Clear LevelTriggeredInterrupt to use edge triggered interrupts when in MSI mode
877 mTimerConfiguration
.Uint64
= HpetRead (HPET_TIMER_CONFIGURATION_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
);
878 mTimerConfiguration
.Bits
.LevelTriggeredInterrupt
= 0;
881 // If no HPET timers support MSI or I/O APIC modes, then ASSERT() and unload the driver.
883 ASSERT (mTimerIndex
!= HPET_INVALID_TIMER_INDEX
);
884 if (mTimerIndex
== HPET_INVALID_TIMER_INDEX
) {
885 DEBUG ((DEBUG_ERROR
, "No HPET timers support MSI or I/O APIC mode. Unload HPET driver.\n"));
886 return EFI_DEVICE_ERROR
;
890 // Initialize I/O APIC entry for HPET Timer Interrupt
891 // Fixed Delivery Mode, Level Triggered, Asserted Low
893 IoApicConfigureInterrupt (mTimerIrq
, PcdGet8 (PcdHpetLocalApicVector
), IO_APIC_DELIVERY_MODE_LOWEST_PRIORITY
, TRUE
, FALSE
);
896 // Read the HPET Timer Capabilities and Configuration register and initialize for I/O APIC mode
897 // Clear MsiInterruptCapability to force rest of driver to use I/O APIC mode
898 // Set LevelTriggeredInterrupt to use level triggered interrupts when in I/O APIC mode
899 // Set InterruptRoute field based in mTimerIrq
901 mTimerConfiguration
.Uint64
= HpetRead (HPET_TIMER_CONFIGURATION_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
);
902 mTimerConfiguration
.Bits
.LevelTriggeredInterrupt
= 1;
903 mTimerConfiguration
.Bits
.InterruptRoute
= mTimerIrq
;
907 // Configure the selected HPET Timer with settings common to both MSI mode and I/O APIC mode
908 // Clear InterruptEnable to keep interrupts disabled until full init is complete
909 // Clear PeriodicInterruptEnable to use one-shot mode
910 // Configure as a 32-bit counter
912 mTimerConfiguration
.Bits
.InterruptEnable
= 0;
913 mTimerConfiguration
.Bits
.PeriodicInterruptEnable
= 0;
914 mTimerConfiguration
.Bits
.CounterSizeEnable
= 1;
915 HpetWrite (HPET_TIMER_CONFIGURATION_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, mTimerConfiguration
.Uint64
);
918 // Read the HPET Timer Capabilities and Configuration register back again.
919 // CounterSizeEnable will be read back as a 0 if it is a 32-bit only timer
921 mTimerConfiguration
.Uint64
= HpetRead (HPET_TIMER_CONFIGURATION_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
);
922 if ((mTimerConfiguration
.Bits
.CounterSizeEnable
== 1) && (sizeof (UINTN
) == sizeof (UINT64
))) {
923 DEBUG ((DEBUG_INFO
, "Choose 64-bit HPET timer.\n"));
925 // 64-bit BIOS can use 64-bit HPET timer
927 mCounterMask
= 0xffffffffffffffffULL
;
929 // Set timer back to 64-bit
931 mTimerConfiguration
.Bits
.CounterSizeEnable
= 0;
932 HpetWrite (HPET_TIMER_CONFIGURATION_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
, mTimerConfiguration
.Uint64
);
934 DEBUG ((DEBUG_INFO
, "Choose 32-bit HPET timer.\n"));
935 mCounterMask
= 0x00000000ffffffffULL
;
939 // Install interrupt handler for selected HPET Timer
941 Status
= mCpu
->RegisterInterruptHandler (mCpu
, PcdGet8 (PcdHpetLocalApicVector
), TimerInterruptHandler
);
942 ASSERT_EFI_ERROR (Status
);
943 if (EFI_ERROR (Status
)) {
944 DEBUG ((DEBUG_ERROR
, "Unable to register HPET interrupt with CPU Arch Protocol. Unload HPET driver.\n"));
945 return EFI_DEVICE_ERROR
;
949 // Force the HPET Timer to be enabled at its default period
951 Status
= TimerDriverSetTimerPeriod (&mTimer
, PcdGet64 (PcdHpetDefaultTimerPeriod
));
952 ASSERT_EFI_ERROR (Status
);
953 if (EFI_ERROR (Status
)) {
954 DEBUG ((DEBUG_ERROR
, "Unable to set HPET default timer rate. Unload HPET driver.\n"));
955 return EFI_DEVICE_ERROR
;
959 // Show state of enabled HPET timer
962 if (mTimerConfiguration
.Bits
.MsiInterruptCapability
!= 0 && FeaturePcdGet (PcdHpetMsiEnable
)) {
963 DEBUG ((DEBUG_INFO
, "HPET Interrupt Mode MSI\n"));
965 DEBUG ((DEBUG_INFO
, "HPET Interrupt Mode I/O APIC\n"));
966 DEBUG ((DEBUG_INFO
, "HPET I/O APIC IRQ = 0x%02x\n", mTimerIrq
));
968 DEBUG ((DEBUG_INFO
, "HPET Interrupt Vector = 0x%02x\n", PcdGet8 (PcdHpetLocalApicVector
)));
969 DEBUG ((DEBUG_INFO
, "HPET Counter Mask = 0x%016lx\n", mCounterMask
));
970 DEBUG ((DEBUG_INFO
, "HPET Timer Period = %d\n", mTimerPeriod
));
971 DEBUG ((DEBUG_INFO
, "HPET Timer Count = 0x%016lx\n", mTimerCount
));
972 DEBUG ((DEBUG_INFO
, "HPET_TIMER%d_CONFIGURATION = 0x%016lx\n", mTimerIndex
, HpetRead (HPET_TIMER_CONFIGURATION_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
)));
973 DEBUG ((DEBUG_INFO
, "HPET_TIMER%d_COMPARATOR = 0x%016lx\n", mTimerIndex
, HpetRead (HPET_TIMER_COMPARATOR_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
)));
974 DEBUG ((DEBUG_INFO
, "HPET_TIMER%d_MSI_ROUTE = 0x%016lx\n", mTimerIndex
, HpetRead (HPET_TIMER_MSI_ROUTE_OFFSET
+ mTimerIndex
* HPET_TIMER_STRIDE
)));
977 // Wait for a few timer interrupts to fire before continuing
979 while (mNumTicks
< 10);
983 // Install the Timer Architectural Protocol onto a new handle
985 Status
= gBS
->InstallMultipleProtocolInterfaces (
987 &gEfiTimerArchProtocolGuid
, &mTimer
,
990 ASSERT_EFI_ERROR (Status
);