]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MtrrLib/MtrrLib.c
UefiCpuPkg/MtrrLib: Add worker functions not invoke IsMtrrSupported()
[mirror_edk2.git] / UefiCpuPkg / Library / MtrrLib / MtrrLib.c
index bcf5e20459c3369e638a11513a5f9ab339abe9ec..b1c12aa32e01026a4a2765b9f08a4bd13d51c39f 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   MTRR setting library\r
 \r
-  Copyright (c) 2008 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2008 - 2015, 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
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
 \r
+//\r
+// Context to save and restore when MTRRs are programmed\r
+//\r
+typedef struct {\r
+  UINTN    Cr4;\r
+  BOOLEAN  InterruptState;\r
+} MTRR_CONTEXT;\r
+\r
 //\r
 // This table defines the offset, base and length of the fixed MTRRs\r
 //\r
@@ -78,7 +86,7 @@ CONST FIXED_MTRR  mMtrrLibFixedMtrrTable[] = {
     MTRR_LIB_IA32_MTRR_FIX4K_F8000,\r
     0xF8000,\r
     SIZE_4KB\r
-  },\r
+  }\r
 };\r
 \r
 //\r
@@ -95,6 +103,24 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mMtrrMemoryCacheTypeShortName[] = {
   "R*"   // Invalid\r
 };\r
 \r
+/**\r
+  Worker function returns the variable MTRR count for the CPU.\r
+\r
+  @return Variable MTRR count\r
+\r
+**/\r
+UINT32\r
+GetVariableMtrrCountWorker (\r
+  VOID\r
+  )\r
+{\r
+  UINT32  VariableMtrrCount;\r
+\r
+  VariableMtrrCount = (UINT32)(AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP) & MTRR_LIB_IA32_MTRR_CAP_VCNT_MASK);\r
+  ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR);\r
+  return VariableMtrrCount;\r
+}\r
+\r
 /**\r
   Returns the variable MTRR count for the CPU.\r
 \r
@@ -107,16 +133,33 @@ GetVariableMtrrCount (
   VOID\r
   )\r
 {\r
-  UINT32  VariableMtrrCount;\r
-\r
   if (!IsMtrrSupported ()) {\r
     return 0;\r
   }\r
+  return GetVariableMtrrCountWorker ();\r
+}\r
 \r
-  VariableMtrrCount = (UINT32)(AsmReadMsr64 (MTRR_LIB_IA32_MTRR_CAP) & MTRR_LIB_IA32_MTRR_CAP_VCNT_MASK);\r
-  ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR);\r
+/**\r
+  Worker function returns the firmware usable variable MTRR count for the CPU.\r
 \r
-  return VariableMtrrCount;\r
+  @return Firmware usable variable MTRR count\r
+\r
+**/\r
+UINT32\r
+GetFirmwareVariableMtrrCountWorker (\r
+  VOID\r
+  )\r
+{\r
+  UINT32  VariableMtrrCount;\r
+  UINT32  ReservedMtrrNumber;\r
+\r
+  VariableMtrrCount = GetVariableMtrrCountWorker ();\r
+  ReservedMtrrNumber = PcdGet32 (PcdCpuNumberOfReservedVariableMtrrs);\r
+  if (VariableMtrrCount < ReservedMtrrNumber) {\r
+    return 0;\r
+  }\r
+\r
+  return VariableMtrrCount - ReservedMtrrNumber;\r
 }\r
 \r
 /**\r
@@ -131,16 +174,27 @@ GetFirmwareVariableMtrrCount (
   VOID\r
   )\r
 {\r
-  UINT32  VariableMtrrCount;\r
-\r
-  VariableMtrrCount = GetVariableMtrrCount ();\r
-  if (VariableMtrrCount < RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER) {\r
+  if (!IsMtrrSupported ()) {\r
     return 0;\r
   }\r
+  return GetFirmwareVariableMtrrCountWorker ();\r
+}\r
+\r
+/**\r
+  Worker function returns the default MTRR cache type for the system.\r
 \r
-  return VariableMtrrCount - RESERVED_FIRMWARE_VARIABLE_MTRR_NUMBER;\r
+  @return  The default MTRR cache type.\r
+\r
+**/\r
+MTRR_MEMORY_CACHE_TYPE\r
+MtrrGetDefaultMemoryTypeWorker (\r
+  VOID\r
+  )\r
+{\r
+  return (MTRR_MEMORY_CACHE_TYPE) (AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE) & 0x7);\r
 }\r
 \r
+\r
 /**\r
   Returns the default MTRR cache type for the system.\r
 \r
@@ -156,8 +210,7 @@ MtrrGetDefaultMemoryType (
   if (!IsMtrrSupported ()) {\r
     return CacheUncacheable;\r
   }\r
-\r
-  return (MTRR_MEMORY_CACHE_TYPE) (AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE) & 0x7);\r
+  return MtrrGetDefaultMemoryTypeWorker ();\r
 }\r
 \r
 /**\r
@@ -166,15 +219,18 @@ MtrrGetDefaultMemoryType (
   This function will do some preparation for programming MTRRs:\r
   disable cache, invalid cache and disable MTRR caching functionality\r
 \r
-  @return  CR4 value before changing.\r
+  @param[out] MtrrContext  Pointer to context to save\r
 \r
 **/\r
-UINTN\r
+VOID\r
 PreMtrrChange (\r
-  VOID\r
+  OUT MTRR_CONTEXT  *MtrrContext\r
   )\r
 {\r
-  UINTN  Value;\r
+  //\r
+  // Disable interrupts and save current interrupt state\r
+  //\r
+  MtrrContext->InterruptState = SaveAndDisableInterrupts();\r
 \r
   //\r
   // Enter no fill cache mode, CD=1(Bit30), NW=0 (Bit29)\r
@@ -184,8 +240,8 @@ PreMtrrChange (
   //\r
   // Save original CR4 value and clear PGE flag (Bit 7)\r
   //\r
-  Value = AsmReadCr4 ();\r
-  AsmWriteCr4 (Value & (~BIT7));\r
+  MtrrContext->Cr4 = AsmReadCr4 ();\r
+  AsmWriteCr4 (MtrrContext->Cr4 & (~BIT7));\r
 \r
   //\r
   // Flush all TLBs\r
@@ -193,38 +249,27 @@ PreMtrrChange (
   CpuFlushTlb ();\r
 \r
   //\r
-  // Disable Mtrrs\r
+  // Disable MTRRs\r
   //\r
   AsmMsrBitFieldWrite64 (MTRR_LIB_IA32_MTRR_DEF_TYPE, 10, 11, 0);\r
-\r
-  //\r
-  // Return original CR4 value\r
-  //\r
-  return Value;\r
 }\r
 \r
-\r
 /**\r
   Cleaning up after programming MTRRs.\r
 \r
   This function will do some clean up after programming MTRRs:\r
-  enable MTRR caching functionality, and enable cache\r
+  Flush all TLBs,  re-enable caching, restore CR4.\r
 \r
-  @param  Cr4  CR4 value to restore\r
+  @param[in] MtrrContext  Pointer to context to restore\r
 \r
 **/\r
 VOID\r
-PostMtrrChange (\r
-  UINTN Cr4\r
+PostMtrrChangeEnableCache (\r
+  IN MTRR_CONTEXT  *MtrrContext\r
   )\r
 {\r
   //\r
-  // Enable Cache MTRR\r
-  //\r
-  AsmMsrBitFieldWrite64 (MTRR_LIB_IA32_MTRR_DEF_TYPE, 10, 11, 3);\r
-\r
-  //\r
-  // Flush all TLBs \r
+  // Flush all TLBs\r
   //\r
   CpuFlushTlb ();\r
 \r
@@ -236,16 +281,43 @@ PostMtrrChange (
   //\r
   // Restore original CR4 value\r
   //\r
-  AsmWriteCr4 (Cr4);\r
+  AsmWriteCr4 (MtrrContext->Cr4);\r
+\r
+  //\r
+  // Restore original interrupt state\r
+  //\r
+  SetInterruptState (MtrrContext->InterruptState);\r
+}\r
+\r
+/**\r
+  Cleaning up after programming MTRRs.\r
+\r
+  This function will do some clean up after programming MTRRs:\r
+  enable MTRR caching functionality, and enable cache\r
+\r
+  @param[in] MtrrContext  Pointer to context to restore\r
+\r
+**/\r
+VOID\r
+PostMtrrChange (\r
+  IN MTRR_CONTEXT  *MtrrContext\r
+  )\r
+{\r
+  //\r
+  // Enable Cache MTRR\r
+  //\r
+  AsmMsrBitFieldWrite64 (MTRR_LIB_IA32_MTRR_DEF_TYPE, 10, 11, 3);\r
+\r
+  PostMtrrChangeEnableCache (MtrrContext);\r
 }\r
 \r
 \r
 /**\r
   Programs fixed MTRRs registers.\r
 \r
-  @param  MemoryCacheType  The memory type to set.\r
-  @param  Base             The base address of memory range.\r
-  @param  Length           The length of memory range.\r
+  @param[in]      MemoryCacheType  The memory type to set.\r
+  @param[in, out] Base             The base address of memory range.\r
+  @param[in, out] Length           The length of memory range.\r
 \r
   @retval RETURN_SUCCESS      The cache type was updated successfully\r
   @retval RETURN_UNSUPPORTED  The requested range or cache type was invalid\r
@@ -327,14 +399,14 @@ ProgramFixedMtrr (
 \r
 \r
 /**\r
-  Get the attribute of variable MTRRs.\r
+  Gets the attribute of variable MTRRs.\r
 \r
   This function shadows the content of variable MTRRs into an\r
   internal array: VariableMtrr.\r
 \r
-  @param  MtrrValidBitsMask     The mask for the valid bit of the MTRR\r
-  @param  MtrrValidAddressMask  The valid address mask for MTRR\r
-  @param  VariableMtrr          The array to shadow variable MTRRs content\r
+  @param[in]   MtrrValidBitsMask     The mask for the valid bit of the MTRR\r
+  @param[in]   MtrrValidAddressMask  The valid address mask for MTRR\r
+  @param[out]  VariableMtrr          The array to shadow variable MTRRs content\r
 \r
   @return                       The return value of this paramter indicates the\r
                                 number of MTRRs which has been used.\r
@@ -394,9 +466,9 @@ MtrrGetMemoryAttributeInVariableMtrr (
 /**\r
   Checks overlap between given memory range and MTRRs.\r
 \r
-  @param  Start            The start address of memory range.\r
-  @param  End              The end address of memory range.\r
-  @param  VariableMtrr     The array to shadow variable MTRRs content\r
+  @param[in]  Start                      The start address of memory range.\r
+  @param[in]  End                        The end address of memory range.\r
+  @param[in]  VariableMtrr               The array to shadow variable MTRRs content\r
 \r
   @retval TRUE             Overlap exists.\r
   @retval FALSE            No overlap.\r
@@ -432,9 +504,9 @@ CheckMemoryAttributeOverlap (
 /**\r
   Marks a variable MTRR as non-valid.\r
 \r
-  @param  Index         The index of the array VariableMtrr to be invalidated\r
-  @param  VariableMtrr  The array to shadow variable MTRRs content\r
-  @param  UsedMtrr      The number of MTRRs which has already been used\r
+  @param[in]   Index         The index of the array VariableMtrr to be invalidated\r
+  @param[in]   VariableMtrr  The array to shadow variable MTRRs content\r
+  @param[out]  UsedMtrr      The number of MTRRs which has already been used\r
 \r
 **/\r
 VOID\r
@@ -450,16 +522,16 @@ InvalidateShadowMtrr (
 \r
 \r
 /**\r
-  Combine memory attributes.\r
+  Combines memory attributes.\r
 \r
   If overlap exists between given memory range and MTRRs, try to combine them.\r
 \r
-  @param  Attributes             The memory type to set.\r
-  @param  Base                   The base address of memory range.\r
-  @param  Length                 The length of memory range.\r
-  @param  VariableMtrr           The array to shadow variable MTRRs content\r
-  @param  UsedMtrr               The number of MTRRs which has already been used\r
-  @param  OverwriteExistingMtrr  Returns whether an existing MTRR was used\r
+  @param[in]       Attributes                 The memory type to set.\r
+  @param[in, out]  Base                       The base address of memory range.\r
+  @param[in, out]  Length                     The length of memory range.\r
+  @param[in]       VariableMtrr               The array to shadow variable MTRRs content\r
+  @param[in, out]  UsedMtrr                   The number of MTRRs which has already been used\r
+  @param[out]      OverwriteExistingMtrr      Returns whether an existing MTRR was used\r
 \r
   @retval EFI_SUCCESS            Memory region successfully combined.\r
   @retval EFI_ACCESS_DENIED      Memory region cannot be combined.\r
@@ -507,7 +579,7 @@ CombineMemoryAttribute (
     //\r
     if (Attributes == VariableMtrr[Index].Type) {\r
       //\r
-      // if the Mtrr range contain the request range, set a flag, then continue to \r
+      // if the MTRR range contain the request range, set a flag, then continue to\r
       // invalidate any MTRR of the same request range with higher priority cache type.\r
       //\r
       if (VariableMtrr[Index].BaseAddress <= *Base && MtrrEnd >= EndAddress) {\r
@@ -568,9 +640,10 @@ CombineMemoryAttribute (
 \r
 \r
 /**\r
-  Calculate the maximum value which is a power of 2, but less the MemoryLength.\r
+  Calculates the maximum value which is a power of 2, but less the MemoryLength.\r
+\r
+  @param[in]  MemoryLength        The number to pass in.\r
 \r
-  @param  MemoryLength        The number to pass in.\r
   @return The maximum value which is align to power of 2 and less the MemoryLength\r
 \r
 **/\r
@@ -597,21 +670,22 @@ Power2MaxMemory (
 \r
 \r
 /**\r
-  Determine the MTRR numbers used to program a memory range.\r
+  Determines the MTRR numbers used to program a memory range.\r
 \r
-  This function first checks the alignment of the base address. If the alignment of the base address <= Length,\r
-  cover the memory range (BaseAddress, alignment) by a MTRR, then BaseAddress += alignment and Length -= alignment.\r
-  Repeat the step until alignment > Length.\r
+  This function first checks the alignment of the base address.\r
+  If the alignment of the base address <= Length, cover the memory range\r
+ (BaseAddress, alignment) by a MTRR, then BaseAddress += alignment and\r
+  Length -= alignment. Repeat the step until alignment > Length.\r
 \r
-  Then this function determines which direction of programming the variable MTRRs for the remaining length\r
-  will use fewer MTRRs.\r
+  Then this function determines which direction of programming the variable\r
+  MTRRs for the remaining length will use fewer MTRRs.\r
 \r
-  @param  BaseAddress Length of Memory to program MTRR\r
-  @param  Length      Length of Memory to program MTRR\r
-  @param  MtrrNumber  Pointer to the number of necessary MTRRs\r
+  @param[in]  BaseAddress Length of Memory to program MTRR\r
+  @param[in]  Length      Length of Memory to program MTRR\r
+  @param[in]  MtrrNumber  Pointer to the number of necessary MTRRs\r
 \r
   @retval TRUE        Positive direction is better.\r
-          FALSE       Negtive direction is better.\r
+          FALSE       Negative direction is better.\r
 \r
 **/\r
 BOOLEAN\r
@@ -680,19 +754,19 @@ GetMtrrNumberAndDirection (
   This function programs MTRRs according to the values specified\r
   in the shadow array.\r
 \r
-  @param  VariableMtrr   The array to shadow variable MTRRs content\r
+  @param[in, out]  VariableMtrr       Shadow of variable MTRR contents\r
 \r
 **/\r
 VOID\r
 InvalidateMtrr (\r
-   IN     VARIABLE_MTRR      *VariableMtrr\r
-   )\r
+  IN OUT VARIABLE_MTRR           *VariableMtrr\r
+  )\r
 {\r
-  UINTN Index;\r
-  UINTN Cr4;\r
-  UINTN VariableMtrrCount;\r
+  UINTN         Index;\r
+  UINTN         VariableMtrrCount;\r
+  MTRR_CONTEXT  MtrrContext;\r
 \r
-  Cr4 = PreMtrrChange ();\r
+  PreMtrrChange (&MtrrContext);\r
   Index = 0;\r
   VariableMtrrCount = GetVariableMtrrCount ();\r
   while (Index < VariableMtrrCount) {\r
@@ -703,7 +777,7 @@ InvalidateMtrr (
     }\r
     Index ++;\r
   }\r
-  PostMtrrChange (Cr4);\r
+  PostMtrrChange (&MtrrContext);\r
 }\r
 \r
 \r
@@ -712,11 +786,11 @@ InvalidateMtrr (
 \r
   This function programs variable MTRRs\r
 \r
-  @param  MtrrNumber            Index of MTRR to program.\r
-  @param  BaseAddress           Base address of memory region.\r
-  @param  Length                Length of memory region.\r
-  @param  MemoryCacheType       Memory type to set.\r
-  @param  MtrrValidAddressMask  The valid address mask for MTRR\r
+  @param[in]       MtrrNumber            Index of MTRR to program.\r
+  @param[in]       BaseAddress           Base address of memory region.\r
+  @param[in]       Length                Length of memory region.\r
+  @param[in]       MemoryCacheType       Memory type to set.\r
+  @param[in]       MtrrValidAddressMask  The valid address mask for MTRR\r
 \r
 **/\r
 VOID\r
@@ -728,10 +802,10 @@ ProgramVariableMtrr (
   IN UINT64                   MtrrValidAddressMask\r
   )\r
 {\r
-  UINT64  TempQword;\r
-  UINTN   Cr4;\r
+  UINT64        TempQword;\r
+  MTRR_CONTEXT  MtrrContext;\r
 \r
-  Cr4 = PreMtrrChange ();\r
+  PreMtrrChange (&MtrrContext);\r
 \r
   //\r
   // MTRR Physical Base\r
@@ -748,14 +822,14 @@ ProgramVariableMtrr (
     (TempQword & MtrrValidAddressMask) | MTRR_LIB_CACHE_MTRR_ENABLED\r
     );\r
 \r
-  PostMtrrChange (Cr4);\r
+  PostMtrrChange (&MtrrContext);\r
 }\r
 \r
 \r
 /**\r
-  Convert the Memory attibute value to MTRR_MEMORY_CACHE_TYPE.\r
+  Converts the Memory attribute value to MTRR_MEMORY_CACHE_TYPE.\r
 \r
-  @param  MtrrType  MTRR memory type\r
+  @param[in]  MtrrType           MTRR memory type\r
 \r
   @return The enum item in MTRR_MEMORY_CACHE_TYPE\r
 \r
@@ -779,9 +853,9 @@ GetMemoryCacheTypeFromMtrrType (
   default:\r
     //\r
     // MtrrType is MTRR_CACHE_INVALID_TYPE, that means\r
-    // no mtrr covers the range\r
+    // no MTRR covers the range\r
     //\r
-    return CacheUncacheable;\r
+    return MtrrGetDefaultMemoryType ();\r
   }\r
 }\r
 \r
@@ -790,8 +864,8 @@ GetMemoryCacheTypeFromMtrrType (
 \r
   This function initializes the valid bits mask and valid address mask for MTRRs.\r
 \r
-  @param  MtrrValidBitsMask     The mask for the valid bit of the MTRR\r
-  @param  MtrrValidAddressMask  The valid address mask for the MTRR\r
+  @param[out]  MtrrValidBitsMask     The mask for the valid bit of the MTRR\r
+  @param[out]  MtrrValidAddressMask  The valid address mask for the MTRR\r
 \r
 **/\r
 VOID\r
@@ -813,28 +887,28 @@ MtrrLibInitializeMtrrMask (
     *MtrrValidBitsMask    = LShiftU64 (1, PhysicalAddressBits) - 1;\r
     *MtrrValidAddressMask = *MtrrValidBitsMask & 0xfffffffffffff000ULL;\r
   } else {\r
-    *MtrrValidBitsMask    = MTRR_LIB_CACHE_VALID_ADDRESS;\r
-    *MtrrValidAddressMask = 0xFFFFFFFF;\r
+    *MtrrValidBitsMask    = MTRR_LIB_MSR_VALID_MASK;\r
+    *MtrrValidAddressMask = MTRR_LIB_CACHE_VALID_ADDRESS;\r
   }\r
 }\r
 \r
 \r
 /**\r
-  Determing the real attribute of a memory range.\r
+  Determines the real attribute of a memory range.\r
 \r
   This function is to arbitrate the real attribute of the memory when\r
-  there are 2 MTRR covers the same memory range.  For further details,\r
+  there are 2 MTRRs covers the same memory range.  For further details,\r
   please refer the IA32 Software Developer's Manual, Volume 3,\r
   Section 10.11.4.1.\r
 \r
-  @param  MtrrType1    the first kind of Memory type\r
-  @param  MtrrType2    the second kind of memory type\r
+  @param[in]  MtrrType1    The first kind of Memory type\r
+  @param[in]  MtrrType2    The second kind of memory type\r
 \r
 **/\r
 UINT64\r
 MtrrPrecedence (\r
-  UINT64    MtrrType1,\r
-  UINT64    MtrrType2\r
+  IN UINT64    MtrrType1,\r
+  IN UINT64    MtrrType2\r
   )\r
 {\r
   UINT64 MtrrType;\r
@@ -894,11 +968,11 @@ MtrrPrecedence (
 /**\r
   This function attempts to set the attributes for a memory range.\r
 \r
-  @param  BaseAddress            The physical address that is the start\r
-                                 address of a memory region.\r
-  @param  Length                 The size in bytes of the memory region.\r
-  @param  Attributes             The bit mask of attributes to set for the\r
-                                 memory region.\r
+  @param[in]       BaseAddress       The physical address that is the start\r
+                                     address of a memory region.\r
+  @param[in]       Length            The size in bytes of the memory region.\r
+  @param[in]       Attribute         The bit mask of attributes to set for the\r
+                                     memory region.\r
 \r
   @retval RETURN_SUCCESS            The attributes were set for the memory\r
                                     region.\r
@@ -937,10 +1011,10 @@ MtrrSetMemoryAttribute (
   UINT32                    UsedMtrr;\r
   UINT64                    MtrrValidBitsMask;\r
   UINT64                    MtrrValidAddressMask;\r
-  UINTN                     Cr4;\r
   BOOLEAN                   OverwriteExistingMtrr;\r
   UINT32                    FirmwareVariableMtrrCount;\r
   UINT32                    VariableMtrrEnd;\r
+  MTRR_CONTEXT              MtrrContext;\r
 \r
   DEBUG((DEBUG_CACHE, "MtrrSetMemoryAttribute() %a:%016lx-%016lx\n", mMtrrMemoryCacheTypeShortName[Attribute], BaseAddress, Length));\r
 \r
@@ -979,9 +1053,9 @@ MtrrSetMemoryAttribute (
   //\r
   Status = RETURN_SUCCESS;\r
   while ((BaseAddress < BASE_1MB) && (Length > 0) && Status == RETURN_SUCCESS) {\r
-    Cr4 = PreMtrrChange ();\r
+    PreMtrrChange (&MtrrContext);\r
     Status = ProgramFixedMtrr (MemoryType, &BaseAddress, &Length);\r
-    PostMtrrChange (Cr4);\r
+    PostMtrrChange (&MtrrContext);\r
     if (RETURN_ERROR (Status)) {\r
       goto Done;\r
     }\r
@@ -1169,9 +1243,9 @@ Done:
 \r
   This function is mainly for debug purpose.\r
 \r
-  @param  Address   The specific address\r
+  @param[in]  Address            The specific address\r
 \r
-  @return Memory cache type of the sepcific address\r
+  @return Memory cache type of the specific address\r
 \r
 **/\r
 MTRR_MEMORY_CACHE_TYPE\r
@@ -1259,26 +1333,21 @@ MtrrGetMemoryAttribute (
 \r
 \r
 /**\r
-  This function will get the raw value in variable MTRRs\r
+  Worker function will get the raw value in variable MTRRs\r
 \r
-  @param  VariableSettings   A buffer to hold variable MTRRs content.\r
+  @param[out] VariableSettings   A buffer to hold variable MTRRs content.\r
 \r
   @return The VariableSettings input pointer\r
 \r
 **/\r
 MTRR_VARIABLE_SETTINGS*\r
-EFIAPI\r
-MtrrGetVariableMtrr (\r
-  OUT MTRR_VARIABLE_SETTINGS         *VariableSettings\r
+MtrrGetVariableMtrrWorker (\r
+  OUT MTRR_VARIABLE_SETTINGS  *VariableSettings\r
   )\r
 {\r
   UINT32  Index;\r
   UINT32  VariableMtrrCount;\r
 \r
-  if (!IsMtrrSupported ()) {\r
-    return VariableSettings;\r
-  }\r
-\r
   VariableMtrrCount = GetVariableMtrrCount ();\r
   ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR);\r
 \r
@@ -1292,11 +1361,34 @@ MtrrGetVariableMtrr (
   return  VariableSettings;\r
 }\r
 \r
+/**\r
+  This function will get the raw value in variable MTRRs\r
+\r
+  @param[out]  VariableSettings   A buffer to hold variable MTRRs content.\r
+\r
+  @return The VariableSettings input pointer\r
+\r
+**/\r
+MTRR_VARIABLE_SETTINGS*\r
+EFIAPI\r
+MtrrGetVariableMtrr (\r
+  OUT MTRR_VARIABLE_SETTINGS         *VariableSettings\r
+  )\r
+{\r
+  if (!IsMtrrSupported ()) {\r
+    return VariableSettings;\r
+  }\r
+\r
+  return MtrrGetVariableMtrrWorker (\r
+           VariableSettings\r
+           );\r
+}\r
+\r
 \r
 /**\r
   Worker function setting variable MTRRs\r
 \r
-  @param  VariableSettings   A buffer to hold variable MTRRs content.\r
+  @param[in]  VariableSettings   A buffer to hold variable MTRRs content.\r
 \r
 **/\r
 VOID\r
@@ -1326,7 +1418,7 @@ MtrrSetVariableMtrrWorker (
 /**\r
   This function sets variable MTRRs\r
 \r
-  @param  VariableSettings   A buffer to hold variable MTRRs content.\r
+  @param[in]  VariableSettings   A buffer to hold variable MTRRs content.\r
 \r
   @return The pointer of VariableSettings\r
 \r
@@ -1337,23 +1429,46 @@ MtrrSetVariableMtrr (
   IN MTRR_VARIABLE_SETTINGS         *VariableSettings\r
   )\r
 {\r
-  UINTN  Cr4;\r
+  MTRR_CONTEXT  MtrrContext;\r
 \r
   if (!IsMtrrSupported ()) {\r
     return VariableSettings;\r
   }\r
 \r
-  Cr4 = PreMtrrChange ();\r
+  PreMtrrChange (&MtrrContext);\r
   MtrrSetVariableMtrrWorker (VariableSettings);\r
-  PostMtrrChange (Cr4);\r
+  PostMtrrChange (&MtrrContext);\r
   return  VariableSettings;\r
 }\r
 \r
+/**\r
+  Worker function gets the content in fixed MTRRs\r
+\r
+  @param[out]  FixedSettings  A buffer to hold fixed MTRRs content.\r
+\r
+  @retval The pointer of FixedSettings\r
+\r
+**/\r
+MTRR_FIXED_SETTINGS*\r
+MtrrGetFixedMtrrWorker (\r
+  OUT MTRR_FIXED_SETTINGS         *FixedSettings\r
+  )\r
+{\r
+  UINT32  Index;\r
+\r
+  for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {\r
+      FixedSettings->Mtrr[Index] =\r
+        AsmReadMsr64 (mMtrrLibFixedMtrrTable[Index].Msr);\r
+  }\r
+\r
+  return FixedSettings;\r
+}\r
+\r
 \r
 /**\r
   This function gets the content in fixed MTRRs\r
 \r
-  @param  FixedSettings  A buffer to hold fixed Mtrrs content.\r
+  @param[out]  FixedSettings  A buffer to hold fixed MTRRs content.\r
 \r
   @retval The pointer of FixedSettings\r
 \r
@@ -1364,24 +1479,17 @@ MtrrGetFixedMtrr (
   OUT MTRR_FIXED_SETTINGS         *FixedSettings\r
   )\r
 {\r
-  UINT32  Index;\r
-\r
   if (!IsMtrrSupported ()) {\r
     return FixedSettings;\r
   }\r
 \r
-  for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {\r
-      FixedSettings->Mtrr[Index] =\r
-        AsmReadMsr64 (mMtrrLibFixedMtrrTable[Index].Msr);\r
-  };\r
-\r
-  return FixedSettings;\r
+  return MtrrGetFixedMtrrWorker (FixedSettings);\r
 }\r
 \r
 /**\r
   Worker function setting fixed MTRRs\r
 \r
-  @param  FixedSettings  A buffer to hold fixed Mtrrs content.\r
+  @param[in]  FixedSettings  A buffer to hold fixed Mtrrs content.\r
 \r
 **/\r
 VOID\r
@@ -1403,7 +1511,7 @@ MtrrSetFixedMtrrWorker (
 /**\r
   This function sets fixed MTRRs\r
 \r
-  @param  FixedSettings  A buffer to hold fixed Mtrrs content.\r
+  @param[in]  FixedSettings  A buffer to hold fixed Mtrrs content.\r
 \r
   @retval The pointer of FixedSettings\r
 \r
@@ -1414,15 +1522,15 @@ MtrrSetFixedMtrr (
   IN MTRR_FIXED_SETTINGS          *FixedSettings\r
   )\r
 {\r
-  UINTN  Cr4;\r
+  MTRR_CONTEXT  MtrrContext;\r
 \r
   if (!IsMtrrSupported ()) {\r
     return FixedSettings;\r
   }\r
 \r
-  Cr4 = PreMtrrChange ();\r
+  PreMtrrChange (&MtrrContext);\r
   MtrrSetFixedMtrrWorker (FixedSettings);\r
-  PostMtrrChange (Cr4);\r
+  PostMtrrChange (&MtrrContext);\r
 \r
   return FixedSettings;\r
 }\r
@@ -1431,7 +1539,7 @@ MtrrSetFixedMtrr (
 /**\r
   This function gets the content in all MTRRs (variable and fixed)\r
 \r
-  @param  MtrrSetting  A buffer to hold all Mtrrs content.\r
+  @param[out]  MtrrSetting  A buffer to hold all Mtrrs content.\r
 \r
   @retval the pointer of MtrrSetting\r
 \r
@@ -1468,7 +1576,7 @@ MtrrGetAllMtrrs (
 /**\r
   This function sets all MTRRs (variable and fixed)\r
 \r
-  @param  MtrrSetting  A buffer holding all MTRRs content.\r
+  @param[in]  MtrrSetting  A buffer holding all MTRRs content.\r
 \r
   @retval The pointer of MtrrSetting\r
 \r
@@ -1479,13 +1587,13 @@ MtrrSetAllMtrrs (
   IN MTRR_SETTINGS                *MtrrSetting\r
   )\r
 {\r
-  UINTN  Cr4;\r
+  MTRR_CONTEXT  MtrrContext;\r
 \r
   if (!IsMtrrSupported ()) {\r
     return MtrrSetting;\r
   }\r
 \r
-  Cr4 = PreMtrrChange ();\r
+  PreMtrrChange (&MtrrContext);\r
 \r
   //\r
   // Set fixed MTRRs\r
@@ -1502,7 +1610,7 @@ MtrrSetAllMtrrs (
   //\r
   AsmWriteMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE, MtrrSetting->MtrrDefType);\r
 \r
-  PostMtrrChange (Cr4);\r
+  PostMtrrChangeEnableCache (&MtrrContext);\r
 \r
   return MtrrSetting;\r
 }\r
@@ -1540,7 +1648,7 @@ MtrrDebugPrintAllMtrrs (
 \r
     DEBUG((DEBUG_CACHE, "MTRR Settings\n"));\r
     DEBUG((DEBUG_CACHE, "=============\n"));\r
-    \r
+\r
     MtrrGetAllMtrrs (&MtrrSettings);\r
     DEBUG((DEBUG_CACHE, "MTRR Default Type: %016lx\n", MtrrSettings.MtrrDefType));\r
     for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {\r
@@ -1567,7 +1675,7 @@ MtrrDebugPrintAllMtrrs (
       MemoryType = (UINTN)(RShiftU64 (MtrrSettings.Fixed.Mtrr[Index], Index1 * 8) & 0xff);\r
         if (MemoryType > CacheWriteBack) {\r
           MemoryType = MTRR_CACHE_INVALID_TYPE;\r
-        }            \r
+        }\r
         if (MemoryType != PreviousMemoryType) {\r
           if (PreviousMemoryType != MTRR_CACHE_INVALID_TYPE) {\r
             DEBUG((DEBUG_CACHE, "%016lx\n", Base - 1));\r
@@ -1582,6 +1690,12 @@ MtrrDebugPrintAllMtrrs (
 \r
     VariableMtrrCount = GetVariableMtrrCount ();\r
 \r
+    Limit        = BIT36 - 1;\r
+    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
+    if (RegEax >= 0x80000008) {\r
+      AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r
+      Limit = LShiftU64 (1, RegEax & 0xff) - 1;\r
+    }\r
     Base = BASE_1MB;\r
     PreviousMemoryType = MTRR_CACHE_INVALID_TYPE;\r
     do {\r
@@ -1597,18 +1711,12 @@ MtrrDebugPrintAllMtrrs (
         PreviousMemoryType = MemoryType;\r
         DEBUG((DEBUG_CACHE, "%a:%016lx-", mMtrrMemoryCacheTypeShortName[MemoryType], Base));\r
       }\r
-      \r
-      RangeBase    = BASE_1MB;        \r
+\r
+      RangeBase    = BASE_1MB;\r
       NoRangeBase  = BASE_1MB;\r
-      Limit        = BIT36 - 1;\r
-      AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
-      if (RegEax >= 0x80000008) {\r
-        AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r
-        Limit = LShiftU64 (1, RegEax & 0xff) - 1;\r
-      }\r
       RangeLimit   = Limit;\r
       NoRangeLimit = Limit;\r
-      \r
+\r
       for (Index = 0, Found = FALSE; Index < VariableMtrrCount; Index++) {\r
         if ((MtrrSettings.Variables.Mtrr[Index].Mask & BIT11) == 0) {\r
           //\r
@@ -1622,7 +1730,7 @@ MtrrDebugPrintAllMtrrs (
         if (Base >= MtrrBase && Base < MtrrLimit) {\r
           Found = TRUE;\r
         }\r
-        \r
+\r
         if (Base >= MtrrBase && MtrrBase > RangeBase) {\r
           RangeBase = MtrrBase;\r
         }\r
@@ -1635,7 +1743,7 @@ MtrrDebugPrintAllMtrrs (
         if (Base < MtrrLimit && MtrrLimit <= RangeLimit) {\r
           RangeLimit = MtrrLimit;\r
         }\r
-        \r
+\r
         if (Base > MtrrLimit && NoRangeBase < MtrrLimit) {\r
           NoRangeBase = MtrrLimit + 1;\r
         }\r
@@ -1643,13 +1751,13 @@ MtrrDebugPrintAllMtrrs (
           NoRangeLimit = MtrrBase - 1;\r
         }\r
       }\r
-      \r
+\r
       if (Found) {\r
         Base = RangeLimit + 1;\r
       } else {\r
         Base = NoRangeLimit + 1;\r
       }\r
-    } while (Found);\r
+    } while (Base < Limit);\r
     DEBUG((DEBUG_CACHE, "%016lx\n\n", Base - 1));\r
   );\r
 }\r