UefiCpuPkg/MtrrLib: Don't report OutOfResource when MTRR is enough
authorRuiyu Ni <ruiyu.ni@intel.com>
Fri, 5 May 2017 02:10:00 +0000 (10:10 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Mon, 8 May 2017 05:35:25 +0000 (13:35 +0800)
The MTRR calculation algorithm contains a bug that when left
subtraction cannot produce better MTRR solution, it forgets
to restore the BaseAddress/Length so that MtrrLibGetMtrrNumber()
returns bigger value of actual required MTRR numbers.
As a result, the MtrrLib reports OutOfResource but actually the
MTRR is enough.

MEMORY_RANGE mC[] = {
  0, 0x100000, CacheUncacheable,
  0x100000, 0x89F00000, CacheWriteBack,
  0x8A000000, 0x75000000, CacheUncacheable,
  0xFF000000, 0x01000000, CacheWriteProtected,
  0x100000000, 0x7F00000000, CacheUncacheable,
  0xFC240000, 0x2000, CacheWriteCombining // <-- trigger the error
};

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Jeff Fan <jeff.fan@intel.com>
UefiCpuPkg/Library/MtrrLib/MtrrLib.c

index 9d1927262a4edee650bd18621fac69cba7f73738..cf1af29936ad85846bcaf198445386ac4c2a5bb4 100644 (file)
@@ -851,6 +851,8 @@ MtrrLibGetMtrrNumber (
   UINT64  SubtractiveLength;\r
   UINT64  BaseAlignment;\r
   UINT32  Index;\r
+  UINT64  OriginalBaseAddress;\r
+  UINT64  OriginalLength;\r
 \r
   *SubLeft = 0;\r
   *SubRight = 0;\r
@@ -861,6 +863,9 @@ MtrrLibGetMtrrNumber (
   // Get the optimal left subtraction solution.\r
   //\r
   if (BaseAddress != 0) {\r
+\r
+    OriginalBaseAddress    = BaseAddress;\r
+    OriginalLength         = Length;\r
     SubtractiveBaseAddress = 0;\r
     SubtractiveLength      = 0;\r
     //\r
@@ -915,7 +920,10 @@ MtrrLibGetMtrrNumber (
     //\r
     if (*SubLeft != 0) {\r
       BaseAddress = SubtractiveBaseAddress;\r
-      Length = SubtractiveLength;\r
+      Length      = SubtractiveLength;\r
+    } else {\r
+      BaseAddress = OriginalBaseAddress;\r
+      Length      = OriginalLength;\r
     }\r
   }\r
 \r