]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MtrrLib/MtrrLib.c
UefiCpuPkg/MtrrLib: Fix bug that may calculate wrong MTRR result
[mirror_edk2.git] / UefiCpuPkg / Library / MtrrLib / MtrrLib.c
index f37b740fdf7db5929d7611f86561ab8b5a29b8d6..1f85ac7e65232bd1294cf2f9a9e1f1b4dd4bc909 100644 (file)
@@ -5,7 +5,7 @@
     Most of services in this library instance are suggested to be invoked by BSP only,\r
     except for MtrrSetAllMtrrs() which is used to sync BSP's MTRR setting to APs.\r
 \r
-  Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2008 - 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
@@ -1570,7 +1570,7 @@ MtrrLibCalculateMtrrs (
   //\r
   VectorCount = VectorIndex + 1;\r
   DEBUG ((\r
-    DEBUG_CACHE, "VectorCount (%016lx - %016lx) = %d\n", \r
+    DEBUG_CACHE, "  VectorCount (%016lx - %016lx) = %d\n",\r
     Ranges[0].BaseAddress, Ranges[RangeCount - 1].BaseAddress + Ranges[RangeCount - 1].Length, VectorCount\r
     ));\r
   ASSERT (VectorCount < MAX_UINT16);\r
@@ -1583,20 +1583,33 @@ MtrrLibCalculateMtrrs (
   Vector[VectorCount - 1].Address = Base1;\r
 \r
   Weight = (UINT8 *) &Vector[VectorCount];\r
-  //\r
-  // Set mandatory weight between any vector to max\r
-  // Set optional weight and between any vector and self->self to 0\r
-  // E.g.:\r
-  //   00 FF FF FF\r
-  //   00 00 FF FF\r
-  //   00 00 00 FF\r
-  //   00 00 00 00\r
-  //\r
   for (VectorIndex = 0; VectorIndex < VectorCount; VectorIndex++) {\r
+    //\r
+    // Set optional weight between vertices and self->self to 0\r
+    //\r
     SetMem (&Weight[M(VectorIndex, 0)], VectorIndex + 1, 0);\r
-    if (VectorIndex != VectorCount - 1) {\r
-      Weight[M (VectorIndex, VectorIndex + 1)] = (DefaultType == Vector[VectorIndex].Type) ? 0 : 1;\r
-      SetMem (&Weight[M (VectorIndex, VectorIndex + 2)], VectorCount - VectorIndex - 2, MAX_WEIGHT);\r
+    //\r
+    // Set mandatory weight between vectors to MAX_WEIGHT\r
+    //\r
+    SetMem (&Weight[M (VectorIndex, VectorIndex + 1)], VectorCount - VectorIndex - 1, MAX_WEIGHT);\r
+\r
+    // Final result looks like:\r
+    //   00 FF FF FF\r
+    //   00 00 FF FF\r
+    //   00 00 00 FF\r
+    //   00 00 00 00\r
+  }\r
+\r
+  //\r
+  // Set mandatory weight and optional weight for adjacent vertices\r
+  //\r
+  for (VectorIndex = 0; VectorIndex < VectorCount - 1; VectorIndex++) {\r
+    if (Vector[VectorIndex].Type != DefaultType) {\r
+      Weight[M (VectorIndex, VectorIndex + 1)] = 1;\r
+      Weight[O (VectorIndex, VectorIndex + 1)] = 0;\r
+    } else {\r
+      Weight[M (VectorIndex, VectorIndex + 1)] = 0;\r
+      Weight[O (VectorIndex, VectorIndex + 1)] = 1;\r
     }\r
   }\r
 \r
@@ -2209,6 +2222,7 @@ MtrrSetMemoryAttributesInMtrrSettings (
   MTRR_CONTEXT              MtrrContext;\r
   BOOLEAN                   MtrrContextValid;\r
 \r
+  Status = RETURN_SUCCESS;\r
   MtrrLibInitializeMtrrMask (&MtrrValidBitsMask, &MtrrValidAddressMask);\r
 \r
   //\r
@@ -2226,24 +2240,48 @@ MtrrSetMemoryAttributesInMtrrSettings (
   Above1MbExist             = FALSE;\r
   OriginalVariableMtrrCount = 0;\r
 \r
+  //\r
+  // 0. Dump the requests.\r
+  //\r
+  DEBUG_CODE (\r
+    DEBUG ((DEBUG_CACHE, "Mtrr: Set Mem Attribute to %a, ScratchSize = %x%a",\r
+            (MtrrSetting == NULL) ? "Hardware" : "Buffer", *ScratchSize,\r
+            (RangeCount <= 1) ? "," : "\n"\r
+            ));\r
+    for (Index = 0; Index < RangeCount; Index++) {\r
+      DEBUG ((DEBUG_CACHE, " %a: [%016lx, %016lx)\n",\r
+              mMtrrMemoryCacheTypeShortName[MIN (Ranges[Index].Type, CacheInvalid)],\r
+              Ranges[Index].BaseAddress, Ranges[Index].BaseAddress + Ranges[Index].Length\r
+              ));\r
+    }\r
+  );\r
+\r
   //\r
   // 1. Validate the parameters.\r
   //\r
+  if (!IsMtrrSupported ()) {\r
+    Status = RETURN_UNSUPPORTED;\r
+    goto Exit;\r
+  }\r
+\r
   for (Index = 0; Index < RangeCount; Index++) {\r
     if (Ranges[Index].Length == 0) {\r
-      return RETURN_INVALID_PARAMETER;\r
+      Status = RETURN_INVALID_PARAMETER;\r
+      goto Exit;\r
     }\r
     if (((Ranges[Index].BaseAddress & ~MtrrValidAddressMask) != 0) ||\r
         ((Ranges[Index].Length & ~MtrrValidAddressMask) != 0)\r
         ) {\r
-      return RETURN_UNSUPPORTED;\r
+      Status = RETURN_UNSUPPORTED;\r
+      goto Exit;\r
     }\r
     if ((Ranges[Index].Type != CacheUncacheable) &&\r
         (Ranges[Index].Type != CacheWriteCombining) &&\r
         (Ranges[Index].Type != CacheWriteThrough) &&\r
         (Ranges[Index].Type != CacheWriteProtected) &&\r
         (Ranges[Index].Type != CacheWriteBack)) {\r
-      return RETURN_INVALID_PARAMETER;\r
+      Status = RETURN_INVALID_PARAMETER;\r
+      goto Exit;\r
     }\r
     if (Ranges[Index].BaseAddress + Ranges[Index].Length > BASE_1MB) {\r
       Above1MbExist = TRUE;\r
@@ -2309,7 +2347,7 @@ MtrrSetMemoryAttributesInMtrrSettings (
       if (Status == RETURN_ALREADY_STARTED) {\r
         Status = RETURN_SUCCESS;\r
       } else if (Status == RETURN_OUT_OF_RESOURCES) {\r
-        return Status;\r
+        goto Exit;\r
       } else {\r
         ASSERT_RETURN_ERROR (Status);\r
         Modified = TRUE;\r
@@ -2327,7 +2365,7 @@ MtrrSetMemoryAttributesInMtrrSettings (
                  WorkingVariableMtrr, FirmwareVariableMtrrCount + 1, &WorkingVariableMtrrCount\r
                  );\r
       if (RETURN_ERROR (Status)) {\r
-        return Status;\r
+        goto Exit;\r
       }\r
 \r
       //\r
@@ -2346,7 +2384,8 @@ MtrrSetMemoryAttributesInMtrrSettings (
       }\r
 \r
       if (WorkingVariableMtrrCount > FirmwareVariableMtrrCount) {\r
-        return RETURN_OUT_OF_RESOURCES;\r
+        Status = RETURN_OUT_OF_RESOURCES;\r
+        goto Exit;\r
       }\r
 \r
       //\r
@@ -2375,7 +2414,7 @@ MtrrSetMemoryAttributesInMtrrSettings (
                Ranges[Index].BaseAddress, Ranges[Index].Length, Ranges[Index].Type\r
                );\r
     if (RETURN_ERROR (Status)) {\r
-      return Status;\r
+      goto Exit;\r
     }\r
   }\r
 \r
@@ -2441,7 +2480,12 @@ MtrrSetMemoryAttributesInMtrrSettings (
     }\r
   }\r
 \r
-  return RETURN_SUCCESS;\r
+Exit:\r
+  DEBUG ((DEBUG_CACHE, "  Result = %r\n", Status));\r
+  if (!RETURN_ERROR (Status)) {\r
+    MtrrDebugPrintAllMtrrsWorker (MtrrSetting);\r
+  }\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -2475,28 +2519,15 @@ MtrrSetMemoryAttributeInMtrrSettings (
   IN MTRR_MEMORY_CACHE_TYPE  Attribute\r
   )\r
 {\r
-  RETURN_STATUS              Status;\r
   UINT8                      Scratch[SCRATCH_BUFFER_SIZE];\r
   UINTN                      ScratchSize;\r
   MTRR_MEMORY_RANGE          Range;\r
 \r
-  if (!IsMtrrSupported ()) {\r
-    return RETURN_UNSUPPORTED;\r
-  }\r
-\r
   Range.BaseAddress = BaseAddress;\r
   Range.Length      = Length;\r
   Range.Type        = Attribute;\r
   ScratchSize = sizeof (Scratch);\r
-  Status = MtrrSetMemoryAttributesInMtrrSettings (MtrrSetting, Scratch, &ScratchSize, &Range, 1);\r
-  DEBUG ((DEBUG_CACHE, "MtrrSetMemoryAttribute(MtrrSettings = %p) %a: [%016lx, %016lx) - %r\n",\r
-          MtrrSetting,\r
-          mMtrrMemoryCacheTypeShortName[Attribute], BaseAddress, BaseAddress + Length, Status));\r
-\r
-  if (!RETURN_ERROR (Status)) {\r
-    MtrrDebugPrintAllMtrrsWorker (MtrrSetting);\r
-  }\r
-  return Status;\r
+  return MtrrSetMemoryAttributesInMtrrSettings (MtrrSetting, Scratch, &ScratchSize, &Range, 1);\r
 }\r
 \r
 /**\r
@@ -2788,6 +2819,7 @@ MtrrDebugPrintAllMtrrsWorker (
     UINT64            MtrrValidBitsMask;\r
     UINT64            MtrrValidAddressMask;\r
     UINT32            VariableMtrrCount;\r
+    BOOLEAN           ContainVariableMtrr;\r
     MTRR_MEMORY_RANGE Ranges[\r
       ARRAY_SIZE (mMtrrLibFixedMtrrTable) * sizeof (UINT64) + 2 * ARRAY_SIZE (Mtrrs->Variables.Mtrr) + 1\r
       ];\r
@@ -2809,13 +2841,13 @@ MtrrDebugPrintAllMtrrsWorker (
     //\r
     // Dump RAW MTRR contents\r
     //\r
-    DEBUG((DEBUG_CACHE, "MTRR Settings\n"));\r
-    DEBUG((DEBUG_CACHE, "=============\n"));\r
-    DEBUG((DEBUG_CACHE, "MTRR Default Type: %016lx\n", Mtrrs->MtrrDefType));\r
+    DEBUG ((DEBUG_CACHE, "MTRR Settings:\n"));\r
+    DEBUG ((DEBUG_CACHE, "=============\n"));\r
+    DEBUG ((DEBUG_CACHE, "MTRR Default Type: %016lx\n", Mtrrs->MtrrDefType));\r
     for (Index = 0; Index < ARRAY_SIZE (mMtrrLibFixedMtrrTable); Index++) {\r
-      DEBUG((DEBUG_CACHE, "Fixed MTRR[%02d]   : %016lx\n", Index, Mtrrs->Fixed.Mtrr[Index]));\r
+      DEBUG ((DEBUG_CACHE, "Fixed MTRR[%02d]   : %016lx\n", Index, Mtrrs->Fixed.Mtrr[Index]));\r
     }\r
-\r
+    ContainVariableMtrr = FALSE;\r
     for (Index = 0; Index < VariableMtrrCount; Index++) {\r
       if (((MSR_IA32_MTRR_PHYSMASK_REGISTER *)&Mtrrs->Variables.Mtrr[Index].Mask)->Bits.V == 0) {\r
         //\r
@@ -2823,18 +2855,22 @@ MtrrDebugPrintAllMtrrsWorker (
         //\r
         continue;\r
       }\r
+      ContainVariableMtrr = TRUE;\r
       DEBUG ((DEBUG_CACHE, "Variable MTRR[%02d]: Base=%016lx Mask=%016lx\n",\r
         Index,\r
         Mtrrs->Variables.Mtrr[Index].Base,\r
         Mtrrs->Variables.Mtrr[Index].Mask\r
         ));\r
     }\r
+    if (!ContainVariableMtrr) {\r
+      DEBUG ((DEBUG_CACHE, "Variable MTRR    : None.\n"));\r
+    }\r
     DEBUG((DEBUG_CACHE, "\n"));\r
 \r
     //\r
     // Dump MTRR setting in ranges\r
     //\r
-    DEBUG((DEBUG_CACHE, "MTRR Ranges\n"));\r
+    DEBUG((DEBUG_CACHE, "Memory Ranges:\n"));\r
     DEBUG((DEBUG_CACHE, "====================================\n"));\r
     MtrrLibInitializeMtrrMask (&MtrrValidBitsMask, &MtrrValidAddressMask);\r
     Ranges[0].BaseAddress = 0;\r