]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MtrrLib/MtrrLib.c
UefiCpuPkg/MtrrLib: Add worker functions to access MTRRs or variable
[mirror_edk2.git] / UefiCpuPkg / Library / MtrrLib / MtrrLib.c
index d29aca2bdcfbb684399a9c1f74ff35e5794d6e2d..4c76abf9eb38258a6f0d8bb36c2bd10786977451 100644 (file)
@@ -183,15 +183,25 @@ GetFirmwareVariableMtrrCount (
 /**\r
   Worker function returns the default MTRR cache type for the system.\r
 \r
+  If MtrrSetting is not NULL, returns the default MTRR cache type from input\r
+  MTRR settings buffer.\r
+  If MtrrSetting is NULL, returns the default MTRR cache type from MSR.\r
+\r
+  @param[in]  MtrrSetting    A buffer holding all MTRRs content.\r
+\r
   @return  The default MTRR cache type.\r
 \r
 **/\r
 MTRR_MEMORY_CACHE_TYPE\r
 MtrrGetDefaultMemoryTypeWorker (\r
-  VOID\r
+  IN MTRR_SETTINGS      *MtrrSetting\r
   )\r
 {\r
-  return (MTRR_MEMORY_CACHE_TYPE) (AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE) & 0x7);\r
+  if (MtrrSetting == NULL) {\r
+    return (MTRR_MEMORY_CACHE_TYPE) (AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE) & 0x7);\r
+  } else {\r
+    return (MTRR_MEMORY_CACHE_TYPE) (MtrrSetting->MtrrDefType & 0x7);\r
+  }\r
 }\r
 \r
 \r
@@ -210,7 +220,7 @@ MtrrGetDefaultMemoryType (
   if (!IsMtrrSupported ()) {\r
     return CacheUncacheable;\r
   }\r
-  return MtrrGetDefaultMemoryTypeWorker ();\r
+  return MtrrGetDefaultMemoryTypeWorker (NULL);\r
 }\r
 \r
 /**\r
@@ -360,6 +370,12 @@ MtrrGetFixedMtrr (
 /**\r
   Worker function will get the raw value in variable MTRRs\r
 \r
+  If MtrrSetting is not NULL, gets the variable MTRRs raw value from input\r
+  MTRR settings buffer.\r
+  If MtrrSetting is NULL, gets the variable MTRRs raw value from MTRRs.\r
+\r
+  @param[in]  MtrrSetting        A buffer holding all MTRRs content.\r
+  @param[in]  VariableMtrrCount  Number of variable MTRRs.\r
   @param[out] VariableSettings   A buffer to hold variable MTRRs content.\r
 \r
   @return The VariableSettings input pointer\r
@@ -367,6 +383,7 @@ MtrrGetFixedMtrr (
 **/\r
 MTRR_VARIABLE_SETTINGS*\r
 MtrrGetVariableMtrrWorker (\r
+  IN  MTRR_SETTINGS           *MtrrSetting,\r
   IN  UINT32                  VariableMtrrCount,\r
   OUT MTRR_VARIABLE_SETTINGS  *VariableSettings\r
   )\r
@@ -376,10 +393,15 @@ MtrrGetVariableMtrrWorker (
   ASSERT (VariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR);\r
 \r
   for (Index = 0; Index < VariableMtrrCount; Index++) {\r
-    VariableSettings->Mtrr[Index].Base =\r
-      AsmReadMsr64 (MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (Index << 1));\r
-    VariableSettings->Mtrr[Index].Mask =\r
-      AsmReadMsr64 (MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (Index << 1) + 1);\r
+    if (MtrrSetting == NULL) {\r
+      VariableSettings->Mtrr[Index].Base =\r
+        AsmReadMsr64 (MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (Index << 1));\r
+      VariableSettings->Mtrr[Index].Mask =\r
+        AsmReadMsr64 (MTRR_LIB_IA32_VARIABLE_MTRR_BASE + (Index << 1) + 1);\r
+    } else {\r
+      VariableSettings->Mtrr[Index].Base = MtrrSetting->Variables.Mtrr[Index].Base;\r
+      VariableSettings->Mtrr[Index].Mask = MtrrSetting->Variables.Mtrr[Index].Mask;\r
+    }\r
   }\r
 \r
   return  VariableSettings;\r
@@ -404,6 +426,7 @@ MtrrGetVariableMtrr (
   }\r
 \r
   return MtrrGetVariableMtrrWorker (\r
+           NULL,\r
            GetVariableMtrrCountWorker (),\r
            VariableSettings\r
            );\r
@@ -575,6 +598,7 @@ MtrrGetMemoryAttributeInVariableMtrr (
   }\r
 \r
   MtrrGetVariableMtrrWorker (\r
+    NULL,\r
     GetVariableMtrrCountWorker (),\r
     &VariableSettings\r
     );\r
@@ -949,6 +973,11 @@ ProgramVariableMtrr (
 /**\r
   Converts the Memory attribute value to MTRR_MEMORY_CACHE_TYPE.\r
 \r
+  If MtrrSetting is not NULL, gets the default memory attribute from input\r
+  MTRR settings buffer.\r
+  If MtrrSetting is NULL, gets the default memory attribute from MSR.\r
+\r
+  @param[in]  MtrrSetting        A buffer holding all MTRRs content.\r
   @param[in]  MtrrType           MTRR memory type\r
 \r
   @return The enum item in MTRR_MEMORY_CACHE_TYPE\r
@@ -956,6 +985,7 @@ ProgramVariableMtrr (
 **/\r
 MTRR_MEMORY_CACHE_TYPE\r
 GetMemoryCacheTypeFromMtrrType (\r
+  IN MTRR_SETTINGS         *MtrrSetting,\r
   IN UINT64                MtrrType\r
   )\r
 {\r
@@ -975,7 +1005,7 @@ GetMemoryCacheTypeFromMtrrType (
     // MtrrType is MTRR_CACHE_INVALID_TYPE, that means\r
     // no MTRR covers the range\r
     //\r
-    return MtrrGetDefaultMemoryType ();\r
+    return MtrrGetDefaultMemoryTypeWorker (MtrrSetting);\r
   }\r
 }\r
 \r
@@ -1084,21 +1114,22 @@ MtrrPrecedence (
   return MtrrType;\r
 }\r
 \r
-\r
-\r
 /**\r
-  This function will get the memory cache type of the specific address.\r
+  Worker function will get the memory cache type of the specific address.\r
 \r
-  This function is mainly for debug purpose.\r
+  If MtrrSetting is not NULL, gets the memory cache type from input\r
+  MTRR settings buffer.\r
+  If MtrrSetting is NULL, gets the memory cache type from MTRRs.\r
 \r
+  @param[in]  MtrrSetting        A buffer holding all MTRRs content.\r
   @param[in]  Address            The specific address\r
 \r
   @return Memory cache type of the specific address\r
 \r
 **/\r
 MTRR_MEMORY_CACHE_TYPE\r
-EFIAPI\r
-MtrrGetMemoryAttribute (\r
+MtrrGetMemoryAttributeByAddressWorker (\r
+  IN MTRR_SETTINGS      *MtrrSetting,\r
   IN PHYSICAL_ADDRESS   Address\r
   )\r
 {\r
@@ -1114,14 +1145,14 @@ MtrrGetMemoryAttribute (
   UINTN                   VariableMtrrCount;\r
   MTRR_VARIABLE_SETTINGS  VariableSettings;\r
 \r
-  if (!IsMtrrSupported ()) {\r
-    return CacheUncacheable;\r
-  }\r
-\r
   //\r
   // Check if MTRR is enabled, if not, return UC as attribute\r
   //\r
-  TempQword = AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE);\r
+  if (MtrrSetting == NULL) {\r
+    TempQword = AsmReadMsr64 (MTRR_LIB_IA32_MTRR_DEF_TYPE);\r
+  } else {\r
+    TempQword = MtrrSetting->MtrrDefType;\r
+  }\r
   MtrrType = MTRR_CACHE_INVALID_TYPE;\r
 \r
   if ((TempQword & MTRR_LIB_CACHE_MTRR_ENABLED) == 0) {\r
@@ -1146,9 +1177,13 @@ MtrrGetMemoryAttribute (
            SubIndex =\r
              ((UINTN)Address - mMtrrLibFixedMtrrTable[Index].BaseAddress) /\r
                mMtrrLibFixedMtrrTable[Index].Length;\r
-           TempQword = AsmReadMsr64 (mMtrrLibFixedMtrrTable[Index].Msr);\r
+           if (MtrrSetting == NULL) {\r
+             TempQword = AsmReadMsr64 (mMtrrLibFixedMtrrTable[Index].Msr);\r
+           } else {\r
+             TempQword = MtrrSetting->Fixed.Mtrr[Index];\r
+           }\r
            MtrrType =  RShiftU64 (TempQword, SubIndex * 8) & 0xFF;\r
-           return GetMemoryCacheTypeFromMtrrType (MtrrType);\r
+           return GetMemoryCacheTypeFromMtrrType (MtrrSetting, MtrrType);\r
          }\r
       }\r
     }\r
@@ -1156,6 +1191,7 @@ MtrrGetMemoryAttribute (
   MtrrLibInitializeMtrrMask(&MtrrValidBitsMask, &MtrrValidAddressMask);\r
 \r
   MtrrGetVariableMtrrWorker (\r
+    MtrrSetting,\r
     GetVariableMtrrCountWorker (),\r
     &VariableSettings\r
     );\r
@@ -1183,12 +1219,35 @@ MtrrGetMemoryAttribute (
       }\r
     }\r
   }\r
-  CacheType = GetMemoryCacheTypeFromMtrrType (MtrrType);\r
+  CacheType = GetMemoryCacheTypeFromMtrrType (MtrrSetting, MtrrType);\r
 \r
   return CacheType;\r
 }\r
 \r
 \r
+/**\r
+  This function will get the memory cache type of the specific address.\r
+\r
+  This function is mainly for debug purpose.\r
+\r
+  @param[in]  Address   The specific address\r
+\r
+  @return Memory cache type of the specific address\r
+\r
+**/\r
+MTRR_MEMORY_CACHE_TYPE\r
+EFIAPI\r
+MtrrGetMemoryAttribute (\r
+  IN PHYSICAL_ADDRESS   Address\r
+  )\r
+{\r
+  if (!IsMtrrSupported ()) {\r
+    return CacheUncacheable;\r
+  }\r
+\r
+  return MtrrGetMemoryAttributeByAddressWorker (NULL, Address);\r
+}\r
+\r
 \r
 /**\r
   This function prints all MTRRs for debugging.\r
@@ -1479,7 +1538,7 @@ MtrrSetMemoryAttribute (
   //\r
   VariableMtrrCount = GetVariableMtrrCountWorker ();\r
   FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCountWorker ();\r
-  MtrrGetVariableMtrrWorker (VariableMtrrCount, &OriginalVariableSettings);\r
+  MtrrGetVariableMtrrWorker (NULL, VariableMtrrCount, &OriginalVariableSettings);\r
   CopyMem (&WorkingVariableSettings, &OriginalVariableSettings, sizeof (WorkingVariableSettings));\r
   ProgramVariableSettings = TRUE;\r
   VariableSettings = &WorkingVariableSettings;\r
@@ -1839,6 +1898,7 @@ MtrrGetAllMtrrs (
   // Get variable MTRRs\r
   //\r
   MtrrGetVariableMtrrWorker (\r
+    NULL,\r
     GetVariableMtrrCountWorker (),\r
     &MtrrSetting->Variables\r
     );\r