]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Removed the assumption on APIC timer initial Count is all 1s and updated it to handle...
authorJeff Fan <jeff.fan@intel.com>
Tue, 27 Aug 2013 07:29:14 +0000 (07:29 +0000)
committervanjeff <vanjeff@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 27 Aug 2013 07:29:14 +0000 (07:29 +0000)
Signed-off-by: Jeff Fan <jeff.fan@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@14603 6f19259b-4bc3-4df7-8a09-765794883524

MdePkg/Library/SecPeiDxeTimerLibCpu/X86TimerLib.c

index ffd6cc27145c77eaedbf3981608ec85ed4a73824..7b2bc54934ef523f65142f62d53eb626d568cd5d 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Timer Library functions built upon local APIC on IA32/x64.\r
 \r
-  Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2013, 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
@@ -87,6 +87,22 @@ InternalX86GetTimerTick (
   return MmioRead32 (ApicBase + APIC_TMCCT);\r
 }\r
 \r
+/**\r
+  Internal function to read the initial timer count of local APIC.\r
+\r
+  @param  ApicBase  The base address of memory mapped registers of local APIC.\r
+\r
+  @return The initial timer count read.\r
+\r
+**/\r
+UINT32\r
+InternalX86GetInitTimerCount (\r
+  IN      UINTN                     ApicBase\r
+  )\r
+{\r
+  return MmioRead32 (ApicBase + APIC_TMICT);\r
+}\r
+\r
 /**\r
   Stalls the CPU for at least the given number of ticks.\r
 \r
@@ -105,22 +121,49 @@ 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 = InternalX86GetTimerTick (ApicBase) - Delay;\r
+  InitCount = InternalX86GetInitTimerCount (ApicBase);\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 (MmioRead32 (ApicBase + APIC_TMICT));\r
-  while (((UINT32)(InternalX86GetTimerTick (ApicBase) - Ticks) & PowerOfTwoCounter) == 0) {\r
-    CpuPause ();\r
-  }\r
+  StartTick  = InternalX86GetTimerTick (ApicBase);\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 - InternalX86GetTimerTick (ApicBase);\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
@@ -242,11 +285,7 @@ GetPerformanceCounterProperties (
   ApicBase = InternalX86GetApicBase ();\r
 \r
   if (StartValue != NULL) {\r
-    *StartValue = MmioRead32 (ApicBase + APIC_TMICT);\r
-    //\r
-    // make sure StartValue is all 1s from High Bit\r
-    //\r
-    ASSERT ((*StartValue & (*StartValue + 1)) == 0);\r
+    *StartValue = (UINT64)InternalX86GetInitTimerCount (ApicBase);\r
   }\r
 \r
   if (EndValue != NULL) {\r