]> 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 ddf90e2a8c0fea3e035a642958549f5020d16889..1f85ac7e65232bd1294cf2f9a9e1f1b4dd4bc909 100644 (file)
@@ -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