smarttimer.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 660 行 · 第 1/2 页

C
660
字号
/*++

Copyright (c) 2006, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             


Module Name:

 SmartTimer.c

Abstract:

  Timer Architectural Protocol as defined in the DXE CIS

--*/

#include "SmartTimer.h"

//
// The handle onto which the Timer Architectural Protocol will be installed
//
EFI_HANDLE                mTimerHandle = NULL;

//
// The Timer Architectural Protocol that this driver produces
//
EFI_TIMER_ARCH_PROTOCOL   mTimer = {
  TimerDriverRegisterHandler,
  TimerDriverSetTimerPeriod,
  TimerDriverGetTimerPeriod,
  TimerDriverGenerateSoftInterrupt
};

//
// Pointer to the CPU Architectural Protocol instance
//
EFI_CPU_ARCH_PROTOCOL     *mCpu;

//
// Pointer to the CPU I/O Protocol instance
//
EFI_CPU_IO_PROTOCOL       *mCpuIo;

EFI_ACPI_DESCRIPTION      *mAcpiDescription;

//
// Pointer to the Legacy 8259 Protocol instance
//
EFI_LEGACY_8259_PROTOCOL  *mLegacy8259;

//
// The notification function to call on every timer interrupt.
// A bug in the compiler prevents us from initializing this here.
//
volatile EFI_TIMER_NOTIFY mTimerNotifyFunction;

//
// The current period of the timer interrupt
//
volatile UINT64           mTimerPeriod = 0;

//
// The time of twice timer interrupt duration
//
volatile UINTN            mPreAcpiTick = 0;

//
// Worker Functions
//
VOID
SetPitCount (
  IN UINT16  Count
  )
/*++

Routine Description:

  Sets the counter value for Timer #0 in a legacy 8254 timer.

Arguments:

  Count - The 16-bit counter value to program into Timer #0 of the legacy 8254 timer.

Returns: 

  None

--*/
{
  UINT8 Data;

  Data = 0x36;
  mCpuIo->Io.Write (mCpuIo, EfiCpuIoWidthUint8, TIMER_CONTROL_PORT, 1, &Data);
  mCpuIo->Io.Write (mCpuIo, EfiCpuIoWidthFifoUint8, TIMER0_COUNT_PORT, 2, &Count);
}

UINT32
GetAcpiTick (
  VOID
  )
/*++

Routine Description:

  Get the current ACPI counter's value

Arguments:

  None

Returns: 

  The value of the counter

--*/
{
  UINT32  Tick;

  switch (mAcpiDescription->PM_TMR_BLK.AddressSpaceId) {
  case ACPI_ADDRESS_ID_IO:
    mCpuIo->Io.Read (mCpuIo, EfiCpuIoWidthUint32, mAcpiDescription->PM_TMR_BLK.Address, 1, &Tick);
    break;
  case ACPI_ADDRESS_ID_MEMORY:
    mCpuIo->Mem.Read (mCpuIo, EfiCpuIoWidthUint32, mAcpiDescription->PM_TMR_BLK.Address, 1, &Tick);
    break;
  default:
    ASSERT (FALSE);
    Tick = 0;
    break;
  }
  
  //
  // Only 23:0 bit is true value
  //
  if (mAcpiDescription->TMR_VAL_EXT == 0) {
    Tick &= 0xffffff;
  }
  return Tick;
}

UINT64
MeasureTimeLost (
  IN UINT64             TimePeriod
  )
/*++

Routine Description:

  Measure the 8254 timer interrupt use the ACPI time counter

Arguments:

    TimePeriod - 8254 timer period

Returns: 

  The real system time pass between the sequence 8254 timer interrupt

--*/
{
  UINT32  CurrentTick;
  UINT64  EndTick;
  UINT64  LostTime;
  UINT64  MaxValue;

  CurrentTick = GetAcpiTick ();
  EndTick     = (UINT64)CurrentTick;

  if (CurrentTick < mPreAcpiTick) {
    if (mAcpiDescription->TMR_VAL_EXT == 0) {
      MaxValue = 0x1000000;
    } else {
      MaxValue = 0x100000000;
    }
    EndTick = (UINT64)CurrentTick + MaxValue;
  }
  //
  // The calculation of the lost system time should be very accurate, we use
  // the shift calcu to make sure the value's accurate:
  // the origenal formula is:
  //                      (EndTick - mPreAcpiTick) * 10,000,000
  //      LostTime = -----------------------------------------------
  //                   (3,579,545 Hz / 1,193,182 Hz) * 1,193,182 Hz
  //
  // Note: the 3,579,545 Hz is the ACPI timer's clock;
  //       the 1,193,182 Hz is the 8254 timer's clock;
  //
  LostTime = RShiftU64 (
              MultU64x32 ((UINT64) (EndTick - mPreAcpiTick),
              46869689) + 0x00FFFFFF,
              24
              );

  if (LostTime != 0) {
    mPreAcpiTick = CurrentTick;
  }

  return LostTime;
}

VOID
TimerInterruptHandler (
  IN EFI_EXCEPTION_TYPE   InterruptType,
  IN EFI_SYSTEM_CONTEXT   SystemContext
  )
/*++

Routine Description:

  8254 Timer #0 Interrupt Handler

Arguments:

  InterruptType - The type of interrupt that occured

  SystemContext - A pointer to the system context when the interrupt occured

Returns: 

  None

--*/
{
  EFI_TPL OriginalTPL;

  OriginalTPL = gBS->RaiseTPL (EFI_TPL_HIGH_LEVEL);

  mLegacy8259->EndOfInterrupt (mLegacy8259, Efi8259Irq0);

  if (mTimerNotifyFunction) {
    //
    // If we have the timer interrupt miss, then we use
    // the platform ACPI time counter to retrieve the time lost
    //
    mTimerNotifyFunction (MeasureTimeLost (mTimerPeriod));
  }

  gBS->RestoreTPL (OriginalTPL);
}

EFI_STATUS
EFIAPI
TimerDriverRegisterHandler (
  IN EFI_TIMER_ARCH_PROTOCOL  *This,
  IN EFI_TIMER_NOTIFY         NotifyFunction
  )
/*++

Routine Description:

  This function registers the handler NotifyFunction so it is called every time 
  the timer interrupt fires.  It also passes the amount of time since the last 
  handler call to the NotifyFunction.  If NotifyFunction is NULL, then the 
  handler is unregistered.  If the handler is registered, then EFI_SUCCESS is 
  returned.  If the CPU does not support registering a timer interrupt handler, 
  then EFI_UNSUPPORTED is returned.  If an attempt is made to register a handler 
  when a handler is already registered, then EFI_ALREADY_STARTED is returned.  
  If an attempt is made to unregister a handler when a handler is not registered, 
  then EFI_INVALID_PARAMETER is returned.  If an error occurs attempting to 
  register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR 
  is returned.

Arguments:

  This           - The EFI_TIMER_ARCH_PROTOCOL instance.

  NotifyFunction - The function to call when a timer interrupt fires.  This 
                   function executes at TPL_HIGH_LEVEL.  The DXE Core will 
                   register a handler for the timer interrupt, so it can know 
                   how much time has passed.  This information is used to 
                   signal timer based events.  NULL will unregister the handler.

Returns: 

  EFI_SUCCESS           - The timer handler was registered.

  EFI_UNSUPPORTED       - The platform does not support timer interrupts.

  EFI_ALREADY_STARTED   - NotifyFunction is not NULL, and a handler is already 
                          registered.

  EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not 
                          previously registered.

  EFI_DEVICE_ERROR      - The timer handler could not be registered.

--*/
{
  //
  // Check for invalid parameters
  //
  if (NotifyFunction == NULL && mTimerNotifyFunction == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  if (NotifyFunction != NULL && mTimerNotifyFunction != NULL) {
    return EFI_ALREADY_STARTED;
  }

  mTimerNotifyFunction = NotifyFunction;

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
TimerDriverSetTimerPeriod (
  IN EFI_TIMER_ARCH_PROTOCOL  *This,
  IN UINT64                   TimerPeriod
  )
/*++

Routine Description:

  This function adjusts the period of timer interrupts to the value specified 
  by TimerPeriod.  If the timer period is updated, then the selected timer 
  period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned.  If 
  the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.  
  If an error occurs while attempting to update the timer period, then the 
  timer hardware will be put back in its state prior to this call, and 
  EFI_DEVICE_ERROR is returned.  If TimerPeriod is 0, then the timer interrupt 
  is disabled.  This is not the same as disabling the CPU's interrupts.  
  Instead, it must either turn off the timer hardware, or it must adjust the 
  interrupt controller so that a CPU interrupt is not generated when the timer 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?