Home | History | Annotate | Download | only in TscTimerLib
      1 /** @file
      2   A Dxe Timer Library implementation which uses the Time Stamp Counter in the processor.
      3 
      4   For Pentium 4 processors, Intel Xeon processors (family [0FH], models [03H and higher]);
      5     for Intel Core Solo and Intel Core Duo processors (family [06H], model [0EH]);
      6     for the Intel Xeon processor 5100 series and Intel Core 2 Duo processors (family [06H], model [0FH]);
      7     for Intel Core 2 and Intel Xeon processors (family [06H], display_model [17H]);
      8     for Intel Atom processors (family [06H], display_model [1CH]):
      9   the time-stamp counter increments at a constant rate.
     10   That rate may be set by the maximum core-clock to bus-clock ratio of the processor or may be set by
     11   the maximum resolved frequency at which the processor is booted. The maximum resolved frequency may
     12   differ from the maximum qualified frequency of the processor.
     13 
     14   The specific processor configuration determines the behavior. Constant TSC behavior ensures that the
     15   duration of each clock tick is uniform and supports the use of the TSC as a wall clock timer even if
     16   the processor core changes frequency. This is the architectural behavior moving forward.
     17 
     18   A Processor's support for invariant TSC is indicated by CPUID.0x80000007.EDX[8].
     19 
     20   Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
     21   This program and the accompanying materials
     22   are licensed and made available under the terms and conditions of the BSD License
     23   which accompanies this distribution. The full text of the license may be found at
     24   http://opensource.org/licenses/bsd-license.php
     25 
     26   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     27   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     28 
     29 **/
     30 
     31 #include <PiDxe.h>
     32 #include <Library/UefiBootServicesTableLib.h>
     33 #include <Library/UefiLib.h>
     34 #include <Library/DebugLib.h>
     35 #include <Guid/TscFrequency.h>
     36 #include "TscTimerLibInternal.h"
     37 
     38 UINT64 mTscFrequency;
     39 
     40 /** The constructor function determines the actual TSC frequency.
     41 
     42   First, Get TSC frequency from system configuration table with TSC frequency GUID,
     43   if the table is not found, install it.
     44   This function will always return EFI_SUCCESS.
     45 
     46   @param  ImageHandle       The firmware allocated handle for the EFI image.
     47   @param  SystemTable       A pointer to the EFI System Table.
     48 
     49   @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
     50 
     51 **/
     52 EFI_STATUS
     53 EFIAPI
     54 DxeTscTimerLibConstructor (
     55   IN EFI_HANDLE        ImageHandle,
     56   IN EFI_SYSTEM_TABLE  *SystemTable
     57   )
     58 {
     59   EFI_STATUS  Status;
     60   UINT64      *TscFrequency;
     61 
     62   TscFrequency = NULL;
     63   //
     64   // Get TSC frequency from system configuration table with TSC frequency GUID.
     65   //
     66   Status = EfiGetSystemConfigurationTable (&gEfiTscFrequencyGuid, (VOID **) &TscFrequency);
     67   if (Status == EFI_SUCCESS) {
     68     ASSERT (TscFrequency != NULL);
     69     mTscFrequency = *TscFrequency;
     70     return EFI_SUCCESS;
     71   }
     72 
     73   //
     74   // TSC frequency GUID system configuration table is not found, install it.
     75   //
     76 
     77   Status = gBS->AllocatePool (EfiBootServicesData, sizeof (UINT64), (VOID **) &TscFrequency);
     78   ASSERT_EFI_ERROR (Status);
     79 
     80   *TscFrequency = InternalCalculateTscFrequency ();
     81   //
     82   // TscFrequency now points to the number of TSC counts per second, install system configuration table for it.
     83   //
     84   gBS->InstallConfigurationTable (&gEfiTscFrequencyGuid, TscFrequency);
     85 
     86   mTscFrequency = *TscFrequency;
     87   return EFI_SUCCESS;
     88 }
     89 
     90 /**  Get TSC frequency.
     91 
     92   @return The number of TSC counts per second.
     93 
     94 **/
     95 UINT64
     96 InternalGetTscFrequency (
     97   VOID
     98   )
     99 {
    100   return mTscFrequency;
    101 }
    102 
    103