Home | History | Annotate | Download | only in IntelPchAcpiTimerLib
      1 /** @file
      2   ICH9 ACPI Timer implements one instance of Timer Library.
      3 
      4 Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
      5 
      6   This program and the accompanying materials are licensed and made available under
      8   the terms and conditions of the BSD License that accompanies this distribution.
     10   The full text of the license may be found at
     12   http://opensource.org/licenses/bsd-license.php.
     14 
     16   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     18   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     20 
     22 
     24 **/
     25 
     26 #include "CommonHeader.h"
     27 
     28 /**
     29   The constructor function enables ACPI IO space.
     30 
     31   If ACPI I/O space not enabled, this function will enable it.
     32   It will always return RETURN_SUCCESS.
     33 
     34   @retval EFI_SUCCESS   The constructor always returns RETURN_SUCCESS.
     35 
     36 **/
     37 RETURN_STATUS
     38 EFIAPI
     39 IntelPchAcpiTimerLibConstructor (
     40   VOID
     41   )
     42 {
     43   if ((PchLpcPciCfg8(R_PCH_LPC_ACPI_BASE) & B_PCH_LPC_ACPI_BASE_EN) == 0) {
     44   //
     45   // If ACPI I/O space is not enabled, program ACPI I/O base address and enable it.
     46   //
     47     MmioWrite16 (
     48       MmPciAddress (
     49         0,
     50         DEFAULT_PCI_BUS_NUMBER_PCH,
     51         PCI_DEVICE_NUMBER_PCH_LPC,
     52         PCI_FUNCTION_NUMBER_PCH_LPC,
     53         R_PCH_LPC_ACPI_BASE
     54         ),
     55       ((PcdGet16 (PcdPchAcpiIoPortBaseAddress) & B_PCH_LPC_ACPI_BASE_BAR) | B_PCH_LPC_ACPI_BASE_EN)
     56     );
     57   }
     58   return RETURN_SUCCESS;
     59 }
     60 
     61 /**
     62   Internal function to read the current tick counter of ACPI.
     63 
     64   Internal function to read the current tick counter of ACPI.
     65 
     66   @return The tick counter read.
     67 
     68 **/
     69 STATIC
     70 UINT32
     71 InternalAcpiGetTimerTick (
     72   VOID
     73   )
     74 {
     75   return IoRead32 (PcdGet16 (PcdPchAcpiIoPortBaseAddress) + R_PCH_ACPI_PM1_TMR);
     76 }
     77 
     78 /**
     79   Stalls the CPU for at least the given number of ticks.
     80 
     81   Stalls the CPU for at least the given number of ticks. It's invoked by
     82   MicroSecondDelay() and NanoSecondDelay().
     83 
     84   @param  Delay     A period of time to delay in ticks.
     85 
     86 **/
     87 STATIC
     88 VOID
     89 InternalAcpiDelay (
     90   IN      UINT32                    Delay
     91   )
     92 {
     93   UINT32                            Ticks;
     94   UINT32                            Times;
     95 
     96   Times    = Delay >> 22;
     97   Delay   &= BIT22 - 1;
     98   do {
     99     //
    100     // The target timer count is calculated here
    101     //
    102     Ticks    = InternalAcpiGetTimerTick () + Delay;
    103     Delay    = BIT22;
    104     //
    105     // Wait until time out
    106     // Delay >= 2^23 could not be handled by this function
    107     // Timer wrap-arounds are handled correctly by this function
    108     //
    109     while (((Ticks - InternalAcpiGetTimerTick ()) & BIT23) == 0) {
    110       CpuPause ();
    111     }
    112   } while (Times-- > 0);
    113 }
    114 
    115 /**
    116   Stalls the CPU for at least the given number of microseconds.
    117 
    118   Stalls the CPU for the number of microseconds specified by MicroSeconds.
    119 
    120   @param  MicroSeconds  The minimum number of microseconds to delay.
    121 
    122   @return MicroSeconds
    123 
    124 **/
    125 UINTN
    126 EFIAPI
    127 MicroSecondDelay (
    128   IN      UINTN                     MicroSeconds
    129   )
    130 {
    131   InternalAcpiDelay (
    132     (UINT32)DivU64x32 (
    133               MultU64x32 (
    134                 MicroSeconds,
    135                 V_PCH_ACPI_PM1_TMR_FREQUENCY
    136                 ),
    137               1000000u
    138               )
    139     );
    140   return MicroSeconds;
    141 }
    142 
    143 /**
    144   Stalls the CPU for at least the given number of nanoseconds.
    145 
    146   Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
    147 
    148   @param  NanoSeconds The minimum number of nanoseconds to delay.
    149 
    150   @return NanoSeconds
    151 
    152 **/
    153 UINTN
    154 EFIAPI
    155 NanoSecondDelay (
    156   IN      UINTN                     NanoSeconds
    157   )
    158 {
    159   InternalAcpiDelay (
    160     (UINT32)DivU64x32 (
    161               MultU64x32 (
    162                 NanoSeconds,
    163                 V_PCH_ACPI_PM1_TMR_FREQUENCY
    164                 ),
    165               1000000000u
    166               )
    167     );
    168   return NanoSeconds;
    169 }
    170 
    171 /**
    172   Retrieves the current value of a 64-bit free running performance counter.
    173 
    174   Retrieves the current value of a 64-bit free running performance counter. The
    175   counter can either count up by 1 or count down by 1. If the physical
    176   performance counter counts by a larger increment, then the counter values
    177   must be translated. The properties of the counter can be retrieved from
    178   GetPerformanceCounterProperties().
    179 
    180   @return The current value of the free running performance counter.
    181 
    182 **/
    183 UINT64
    184 EFIAPI
    185 GetPerformanceCounter (
    186   VOID
    187   )
    188 {
    189   return (UINT64)InternalAcpiGetTimerTick ();
    190 }
    191 
    192 /**
    193   Retrieves the 64-bit frequency in Hz and the range of performance counter
    194   values.
    195 
    196   If StartValue is not NULL, then the value that the performance counter starts
    197   with immediately after is it rolls over is returned in StartValue. If
    198   EndValue is not NULL, then the value that the performance counter end with
    199   immediately before it rolls over is returned in EndValue. The 64-bit
    200   frequency of the performance counter in Hz is always returned. If StartValue
    201   is less than EndValue, then the performance counter counts up. If StartValue
    202   is greater than EndValue, then the performance counter counts down. For
    203   example, a 64-bit free running counter that counts up would have a StartValue
    204   of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
    205   that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
    206 
    207   @param  StartValue  The value the performance counter starts with when it
    208                       rolls over.
    209   @param  EndValue    The value that the performance counter ends with before
    210                       it rolls over.
    211 
    212   @return             The frequency in Hz.
    213 
    214 **/
    215 UINT64
    216 EFIAPI
    217 GetPerformanceCounterProperties (
    218   OUT      UINT64                    *StartValue,  OPTIONAL
    219   OUT      UINT64                    *EndValue     OPTIONAL
    220   )
    221 {
    222   if (StartValue != NULL) {
    223     *StartValue = 0;
    224   }
    225 
    226   if (EndValue != NULL) {
    227     *EndValue = V_PCH_ACPI_PM1_TMR_MAX_VAL - 1;
    228   }
    229 
    230   return V_PCH_ACPI_PM1_TMR_FREQUENCY;
    231 }
    232 
    233 /**
    234   Converts elapsed ticks of performance counter to time in nanoseconds.
    235 
    236   This function converts the elapsed ticks of running performance counter to
    237   time value in unit of nanoseconds.
    238 
    239   @param  Ticks     The number of elapsed ticks of running performance counter.
    240 
    241   @return           The elapsed time in nanoseconds.
    242 
    243 **/
    244 UINT64
    245 EFIAPI
    246 GetTimeInNanoSecond (
    247   IN      UINT64                     Ticks
    248   )
    249 {
    250   UINT64  NanoSeconds;
    251   UINT32  Remainder;
    252 
    253   //
    254   //          Ticks
    255   // Time = --------- x 1,000,000,000
    256   //        Frequency
    257   //
    258   NanoSeconds = MultU64x32 (DivU64x32Remainder (Ticks, V_PCH_ACPI_PM1_TMR_FREQUENCY, &Remainder), 1000000000u);
    259 
    260   //
    261   // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)
    262   // will not overflow 64-bit.
    263   //
    264   NanoSeconds += DivU64x32 (MultU64x32 ((UINT64) Remainder, 1000000000u), V_PCH_ACPI_PM1_TMR_FREQUENCY);
    265 
    266   return NanoSeconds;
    267 }
    268 
    269 
    270