Home | History | Annotate | Download | only in DxeTimerLib
      1 /** @file
      2   A non-functional instance of the Timer Library.
      3 
      4   Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
      5   This program and the accompanying materials
      6   are licensed and made available under the terms and conditions of the BSD License
      7   which accompanies this distribution.  The full text of the license may be found at
      8   http://opensource.org/licenses/bsd-license.php.
      9 
     10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     12 
     13 **/
     14 
     15 #include <PiPei.h>
     16 #include <Library/BaseLib.h>
     17 #include <Library/TimerLib.h>
     18 #include <Library/DebugLib.h>
     19 #include <Library/EmuThunkLib.h>
     20 #include <Library/UefiBootServicesTableLib.h>
     21 #include <Library/UefiLib.h>
     22 
     23 #include <Protocol/Timer.h>
     24 
     25 
     26 STATIC UINT64                  gTimerPeriod = 0;
     27 STATIC EFI_TIMER_ARCH_PROTOCOL *gTimerAp = NULL;
     28 STATIC EFI_EVENT               gTimerEvent = NULL;
     29 STATIC VOID                    *gRegistration = NULL;
     30 
     31 VOID
     32 EFIAPI
     33 RegisterTimerArchProtocol (
     34   IN EFI_EVENT     Event,
     35   IN VOID          *Context
     36   )
     37 {
     38   EFI_STATUS  Status;
     39 
     40   Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&gTimerAp);
     41   if (!EFI_ERROR (Status)) {
     42     Status = gTimerAp->GetTimerPeriod (gTimerAp, &gTimerPeriod);
     43     ASSERT_EFI_ERROR (Status);
     44 
     45     // Convert to Nanoseconds.
     46     gTimerPeriod = MultU64x32 (gTimerPeriod, 100);
     47 
     48     if (gTimerEvent == NULL) {
     49       Status = gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, (VOID **)&gTimerEvent);
     50       ASSERT_EFI_ERROR (Status);
     51     }
     52   }
     53 }
     54 
     55 
     56 
     57 /**
     58   Stalls the CPU for at least the given number of microseconds.
     59 
     60   Stalls the CPU for the number of microseconds specified by MicroSeconds.
     61 
     62   @param  MicroSeconds  The minimum number of microseconds to delay.
     63 
     64   @return The value of MicroSeconds inputted.
     65 
     66 **/
     67 UINTN
     68 EFIAPI
     69 MicroSecondDelay (
     70   IN      UINTN                     MicroSeconds
     71   )
     72 {
     73   return NanoSecondDelay (MicroSeconds * 1000);
     74 }
     75 
     76 
     77 /**
     78   Stalls the CPU for at least the given number of nanoseconds.
     79 
     80   Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
     81 
     82   @param  NanoSeconds The minimum number of nanoseconds to delay.
     83 
     84   @return The value of NanoSeconds inputted.
     85 
     86 **/
     87 UINTN
     88 EFIAPI
     89 NanoSecondDelay (
     90   IN      UINTN                     NanoSeconds
     91   )
     92 {
     93   EFI_STATUS  Status;
     94   UINT64      HundredNanoseconds;
     95   UINTN       Index;
     96 
     97   if ((gTimerPeriod != 0) &&
     98       ((UINT64)NanoSeconds > gTimerPeriod) &&
     99       (EfiGetCurrentTpl () == TPL_APPLICATION)) {
    100     //
    101     // This stall is long, so use gBS->WaitForEvent () to yield CPU to DXE Core
    102     //
    103 
    104     HundredNanoseconds = DivU64x32 (NanoSeconds, 100);
    105     Status = gBS->SetTimer (gTimerEvent, TimerRelative, HundredNanoseconds);
    106     ASSERT_EFI_ERROR (Status);
    107 
    108     Status = gBS->WaitForEvent (sizeof (gTimerEvent)/sizeof (EFI_EVENT), &gTimerEvent, &Index);
    109     ASSERT_EFI_ERROR (Status);
    110 
    111   } else {
    112     gEmuThunk->Sleep (NanoSeconds);
    113   }
    114   return NanoSeconds;
    115 }
    116 
    117 
    118 /**
    119   Retrieves the current value of a 64-bit free running performance counter.
    120 
    121   The counter can either count up by 1 or count down by 1. If the physical
    122   performance counter counts by a larger increment, then the counter values
    123   must be translated. The properties of the counter can be retrieved from
    124   GetPerformanceCounterProperties().
    125 
    126   @return The current value of the free running performance counter.
    127 
    128 **/
    129 UINT64
    130 EFIAPI
    131 GetPerformanceCounter (
    132   VOID
    133   )
    134 {
    135   return gEmuThunk->QueryPerformanceCounter ();
    136 }
    137 
    138 /**
    139   Retrieves the 64-bit frequency in Hz and the range of performance counter
    140   values.
    141 
    142   If StartValue is not NULL, then the value that the performance counter starts
    143   with immediately after is it rolls over is returned in StartValue. If
    144   EndValue is not NULL, then the value that the performance counter end with
    145   immediately before it rolls over is returned in EndValue. The 64-bit
    146   frequency of the performance counter in Hz is always returned. If StartValue
    147   is less than EndValue, then the performance counter counts up. If StartValue
    148   is greater than EndValue, then the performance counter counts down. For
    149   example, a 64-bit free running counter that counts up would have a StartValue
    150   of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
    151   that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
    152 
    153   @param  StartValue  The value the performance counter starts with when it
    154                       rolls over.
    155   @param  EndValue    The value that the performance counter ends with before
    156                       it rolls over.
    157 
    158   @return The frequency in Hz.
    159 
    160 **/
    161 UINT64
    162 EFIAPI
    163 GetPerformanceCounterProperties (
    164   OUT      UINT64                    *StartValue,  OPTIONAL
    165   OUT      UINT64                    *EndValue     OPTIONAL
    166   )
    167 {
    168 
    169   if (StartValue != NULL) {
    170     *StartValue = 0ULL;
    171   }
    172   if (EndValue != NULL) {
    173     *EndValue = (UINT64)-1LL;
    174   }
    175 
    176   return gEmuThunk->QueryPerformanceFrequency ();
    177 }
    178 
    179 
    180 /**
    181   Register for the Timer AP protocol.
    182 
    183   @param  ImageHandle   The firmware allocated handle for the EFI image.
    184   @param  SystemTable   A pointer to the EFI System Table.
    185 
    186   @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
    187 
    188 **/
    189 EFI_STATUS
    190 EFIAPI
    191 DxeTimerLibConstructor (
    192   IN EFI_HANDLE        ImageHandle,
    193   IN EFI_SYSTEM_TABLE  *SystemTable
    194   )
    195 {
    196   EfiCreateProtocolNotifyEvent (
    197     &gEfiTimerArchProtocolGuid,
    198     TPL_CALLBACK,
    199     RegisterTimerArchProtocol,
    200     NULL,
    201     &gRegistration
    202     );
    203 
    204   return EFI_SUCCESS;
    205 }
    206 
    207