]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/SecPeiDxeTimerLibUefiCpu/X86TimerLib.c
UefiCpuPkg: Clean up source files
[mirror_edk2.git] / UefiCpuPkg / Library / SecPeiDxeTimerLibUefiCpu / X86TimerLib.c
index 3ca3ca8008f705adac92cd25738e2c0151e8fabe..801ebdb1917b98651aa768cfeb3448274187381e 100644 (file)
@@ -2,8 +2,8 @@
   Timer Library functions built upon local APIC on IA32/x64.\r
 \r
   This library uses the local APIC library so that it supports x2APIC mode.\r
-  \r
-  Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
+\r
+  Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -45,6 +45,9 @@ InternalX86GetTimerFrequency (
   Stalls the CPU for at least the given number of ticks. It's invoked by\r
   MicroSecondDelay() and NanoSecondDelay().\r
 \r
+  This function will ASSERT if the APIC timer intial count returned from\r
+  GetApicTimerInitCount() is zero.\r
+\r
   @param  Delay     A period of time to delay in ticks.\r
 \r
 **/\r
@@ -55,22 +58,50 @@ InternalX86Delay (
   )\r
 {\r
   INT32                             Ticks;\r
-  UINT32                            PowerOfTwoCounter;\r
+  UINT32                            Times;\r
+  UINT32                            InitCount;\r
+  UINT32                            StartTick;\r
 \r
   //\r
-  // The target timer count is calculated here\r
+  // In case Delay is too larger, separate it into several small delay slot.\r
+  // Devided Delay by half value of Init Count is to avoid Delay close to\r
+  // the Init Count, timeout maybe missing if the time consuming between 2\r
+  // GetApicTimerCurrentCount() invoking is larger than the time gap between\r
+  // Delay and the Init Count.\r
   //\r
-  Ticks = GetApicTimerCurrentCount () - Delay;\r
+  InitCount = GetApicTimerInitCount ();\r
+  ASSERT (InitCount != 0);\r
+  Times     = Delay / (InitCount / 2);\r
+  Delay     = Delay % (InitCount / 2);\r
 \r
   //\r
-  // Wait until time out\r
-  // Delay > 2^31 could not be handled by this function\r
-  // Timer wrap-arounds are handled correctly by this function\r
+  // Get Start Tick and do delay\r
   //\r
-  PowerOfTwoCounter = GetPowerOfTwo32 (GetApicTimerInitCount ());\r
-  while (((UINT32)(GetApicTimerCurrentCount () - Ticks) & PowerOfTwoCounter) == 0) {\r
-    CpuPause ();\r
-  }\r
+  StartTick  = GetApicTimerCurrentCount ();\r
+  do {\r
+    //\r
+    // Wait until time out by Delay value\r
+    //\r
+    do {\r
+      CpuPause ();\r
+      //\r
+      // Get Ticks from Start to Current.\r
+      //\r
+      Ticks = StartTick - GetApicTimerCurrentCount ();\r
+      //\r
+      // Ticks < 0 means Timer wrap-arounds happens.\r
+      //\r
+      if (Ticks < 0) {\r
+        Ticks += InitCount;\r
+      }\r
+    } while ((UINT32)Ticks < Delay);\r
+\r
+    //\r
+    // Update StartTick and Delay for next delay slot\r
+    //\r
+    StartTick -= (StartTick > Delay) ?  Delay : (Delay - InitCount);\r
+    Delay      = InitCount / 2;\r
+  } while (Times-- > 0);\r
 }\r
 \r
 /**\r
@@ -181,10 +212,6 @@ GetPerformanceCounterProperties (
 {\r
   if (StartValue != NULL) {\r
     *StartValue = (UINT64)GetApicTimerInitCount ();\r
-    //\r
-    // make sure StartValue is all 1s from High Bit\r
-    //\r
-    ASSERT ((*StartValue & (*StartValue + 1)) == 0);\r
   }\r
 \r
   if (EndValue != NULL) {\r
@@ -193,3 +220,47 @@ GetPerformanceCounterProperties (
 \r
   return (UINT64) InternalX86GetTimerFrequency ();\r
 }\r
+\r
+/**\r
+  Converts elapsed ticks of performance counter to time in nanoseconds.\r
+\r
+  This function converts the elapsed ticks of running performance counter to\r
+  time value in unit of nanoseconds.\r
+\r
+  @param  Ticks     The number of elapsed ticks of running performance counter.\r
+\r
+  @return The elapsed time in nanoseconds.\r
+\r
+**/\r
+UINT64\r
+EFIAPI\r
+GetTimeInNanoSecond (\r
+  IN      UINT64                     Ticks\r
+  )\r
+{\r
+  UINT64  Frequency;\r
+  UINT64  NanoSeconds;\r
+  UINT64  Remainder;\r
+  INTN    Shift;\r
+\r
+  Frequency = GetPerformanceCounterProperties (NULL, NULL);\r
+\r
+  //\r
+  //          Ticks\r
+  // Time = --------- x 1,000,000,000\r
+  //        Frequency\r
+  //\r
+  NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);\r
+\r
+  //\r
+  // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.\r
+  // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,\r
+  // i.e. highest bit set in Remainder should <= 33.\r
+  //\r
+  Shift = MAX (0, HighBitSet64 (Remainder) - 33);\r
+  Remainder = RShiftU64 (Remainder, (UINTN) Shift);\r
+  Frequency = RShiftU64 (Frequency, (UINTN) Shift);\r
+  NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);\r
+\r
+  return NanoSeconds;\r
+}\r