]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciRootBridgeIo.c
MdeModulePkg/PciHostBridge: Count the (mm)io overhead when polling
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / PciHostBridgeDxe / PciRootBridgeIo.c
index 5764c2f49f9214e884d93cb24b278a4357c3ffbb..83f553a0a3291989d06c4efa95a643c21661e1c7 100644 (file)
@@ -2,7 +2,7 @@
 \r
   PCI Root Bridge Io Protocol code.\r
 \r
-Copyright (c) 1999 - 2017, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 1999 - 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
@@ -468,6 +468,85 @@ RootBridgeIoGetMemTranslationByAddress (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Return the result of (Multiplicand * Multiplier / Divisor).\r
+\r
+  @param Multiplicand A 64-bit unsigned value.\r
+  @param Multiplier   A 64-bit unsigned value.\r
+  @param Divisor      A 32-bit unsigned value.\r
+  @param Remainder    A pointer to a 32-bit unsigned value. This parameter is\r
+                      optional and may be NULL.\r
+\r
+  @return Multiplicand * Multiplier / Divisor.\r
+**/\r
+UINT64\r
+MultThenDivU64x64x32 (\r
+  IN      UINT64                    Multiplicand,\r
+  IN      UINT64                    Multiplier,\r
+  IN      UINT32                    Divisor,\r
+  OUT     UINT32                    *Remainder  OPTIONAL\r
+  )\r
+{\r
+  UINT64                            Uint64;\r
+  UINT32                            LocalRemainder;\r
+  UINT32                            Uint32;\r
+  if (Multiplicand > DivU64x64Remainder (MAX_UINT64, Multiplier, NULL)) {\r
+    //\r
+    // Make sure Multiplicand is the bigger one.\r
+    //\r
+    if (Multiplicand < Multiplier) {\r
+      Uint64       = Multiplicand;\r
+      Multiplicand = Multiplier;\r
+      Multiplier   = Uint64;\r
+    }\r
+    //\r
+    // Because Multiplicand * Multiplier overflows,\r
+    //   Multiplicand * Multiplier / Divisor\r
+    // = (2 * Multiplicand' + 1) * Multiplier / Divisor\r
+    // = 2 * (Multiplicand' * Multiplier / Divisor) + Multiplier / Divisor\r
+    //\r
+    Uint64 = MultThenDivU64x64x32 (RShiftU64 (Multiplicand, 1), Multiplier, Divisor, &LocalRemainder);\r
+    Uint64 = LShiftU64 (Uint64, 1);\r
+    Uint32 = 0;\r
+    if ((Multiplicand & 0x1) == 1) {\r
+      Uint64 += DivU64x32Remainder (Multiplier, Divisor, &Uint32);\r
+    }\r
+    return Uint64 + DivU64x32Remainder (Uint32 + LShiftU64 (LocalRemainder, 1), Divisor, Remainder);\r
+  } else {\r
+    return DivU64x32Remainder (MultU64x64 (Multiplicand, Multiplier), Divisor, Remainder);\r
+  }\r
+}\r
+\r
+/**\r
+  Return the elapsed tick count from CurrentTick.\r
+\r
+  @param  CurrentTick  On input, the previous tick count.\r
+                       On output, the current tick count.\r
+  @param  StartTick    The value the performance counter starts with when it\r
+                       rolls over.\r
+  @param  EndTick      The value that the performance counter ends with before\r
+                       it rolls over.\r
+\r
+  @return  The elapsed tick count from CurrentTick.\r
+**/\r
+UINT64\r
+GetElapsedTick (\r
+  UINT64  *CurrentTick,\r
+  UINT64  StartTick,\r
+  UINT64  EndTick\r
+  )\r
+{\r
+  UINT64  PreviousTick;\r
+  \r
+  PreviousTick = *CurrentTick;\r
+  *CurrentTick = GetPerformanceCounter();\r
+  if (StartTick < EndTick) {\r
+    return *CurrentTick - PreviousTick;\r
+  } else {\r
+    return PreviousTick - *CurrentTick;\r
+  }\r
+}\r
+\r
 /**\r
   Polls an address in memory mapped I/O space until an exit condition is met,\r
   or a timeout occurs.\r
@@ -517,6 +596,11 @@ RootBridgeIoPollMem (
   EFI_STATUS  Status;\r
   UINT64      NumberOfTicks;\r
   UINT32      Remainder;\r
+  UINT64      StartTick;\r
+  UINT64      EndTick;\r
+  UINT64      CurrentTick;\r
+  UINT64      ElapsedTick;\r
+  UINT64      Frequency;\r
 \r
   if (Result == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -542,28 +626,18 @@ RootBridgeIoPollMem (
     return EFI_SUCCESS;\r
 \r
   } else {\r
-\r
-    //\r
-    // Determine the proper # of metronome ticks to wait for polling the\r
-    // location.  The nuber of ticks is Roundup (Delay /\r
-    // mMetronome->TickPeriod)+1\r
-    // The "+1" to account for the possibility of the first tick being short\r
-    // because we started in the middle of a tick.\r
     //\r
-    // BugBug: overriding mMetronome->TickPeriod with UINT32 until Metronome\r
-    // protocol definition is updated.\r
+    // NumberOfTicks = Frenquency * Delay / EFI_TIMER_PERIOD_SECONDS(1)\r
     //\r
-    NumberOfTicks = DivU64x32Remainder (Delay, (UINT32) mMetronome->TickPeriod,\r
-                      &Remainder);\r
-    if (Remainder != 0) {\r
-      NumberOfTicks += 1;\r
+    Frequency     = GetPerformanceCounterProperties (&StartTick, &EndTick);\r
+    NumberOfTicks = MultThenDivU64x64x32 (Frequency, Delay, (UINT32)EFI_TIMER_PERIOD_SECONDS(1), &Remainder);\r
+    if (Remainder >= (UINTN)EFI_TIMER_PERIOD_SECONDS(1) / 2) {\r
+      NumberOfTicks++;\r
     }\r
-    NumberOfTicks += 1;\r
-\r
-    while (NumberOfTicks != 0) {\r
-\r
-      mMetronome->WaitForTick (mMetronome, 1);\r
-\r
+    for ( ElapsedTick = 0, CurrentTick = GetPerformanceCounter()\r
+        ; ElapsedTick <= NumberOfTicks\r
+        ; ElapsedTick += GetElapsedTick (&CurrentTick, StartTick, EndTick)\r
+        ) {\r
       Status = This->Mem.Read (This, Width, Address, 1, Result);\r
       if (EFI_ERROR (Status)) {\r
         return Status;\r
@@ -572,8 +646,6 @@ RootBridgeIoPollMem (
       if ((*Result & Mask) == Value) {\r
         return EFI_SUCCESS;\r
       }\r
-\r
-      NumberOfTicks -= 1;\r
     }\r
   }\r
   return EFI_TIMEOUT;\r
@@ -626,6 +698,11 @@ RootBridgeIoPollIo (
   EFI_STATUS  Status;\r
   UINT64      NumberOfTicks;\r
   UINT32      Remainder;\r
+  UINT64      StartTick;\r
+  UINT64      EndTick;\r
+  UINT64      CurrentTick;\r
+  UINT64      ElapsedTick;\r
+  UINT64      Frequency;\r
 \r
   //\r
   // No matter what, always do a single poll.\r
@@ -651,25 +728,18 @@ RootBridgeIoPollIo (
     return EFI_SUCCESS;\r
 \r
   } else {\r
-\r
     //\r
-    // Determine the proper # of metronome ticks to wait for polling the\r
-    // location.  The number of ticks is Roundup (Delay /\r
-    // mMetronome->TickPeriod)+1\r
-    // The "+1" to account for the possibility of the first tick being short\r
-    // because we started in the middle of a tick.\r
+    // NumberOfTicks = Frenquency * Delay / EFI_TIMER_PERIOD_SECONDS(1)\r
     //\r
-    NumberOfTicks = DivU64x32Remainder (Delay, (UINT32)mMetronome->TickPeriod,\r
-                      &Remainder);\r
-    if (Remainder != 0) {\r
-      NumberOfTicks += 1;\r
+    Frequency     = GetPerformanceCounterProperties (&StartTick, &EndTick);\r
+    NumberOfTicks = MultThenDivU64x64x32 (Frequency, Delay, (UINT32)EFI_TIMER_PERIOD_SECONDS(1), &Remainder);\r
+    if (Remainder >= (UINTN)EFI_TIMER_PERIOD_SECONDS(1) / 2) {\r
+      NumberOfTicks++;\r
     }\r
-    NumberOfTicks += 1;\r
-\r
-    while (NumberOfTicks != 0) {\r
-\r
-      mMetronome->WaitForTick (mMetronome, 1);\r
-\r
+    for ( ElapsedTick = 0, CurrentTick = GetPerformanceCounter()\r
+        ; ElapsedTick <= NumberOfTicks\r
+        ; ElapsedTick += GetElapsedTick (&CurrentTick, StartTick, EndTick)\r
+        ) {\r
       Status = This->Io.Read (This, Width, Address, 1, Result);\r
       if (EFI_ERROR (Status)) {\r
         return Status;\r
@@ -678,8 +748,6 @@ RootBridgeIoPollIo (
       if ((*Result & Mask) == Value) {\r
         return EFI_SUCCESS;\r
       }\r
-\r
-      NumberOfTicks -= 1;\r
     }\r
   }\r
   return EFI_TIMEOUT;\r