]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MtrrLib/MtrrLib.c
Add DEBUG() macros for DEBUG_CACHE to MTRR Library show all changes memory caches...
[mirror_edk2.git] / UefiCpuPkg / Library / MtrrLib / MtrrLib.c
index fca9258ed7aef8142f484813969c8fd433a9f841..3cf5f1392a652b00c54de71afb44ffed62b0eaa7 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   MTRR setting library\r
 \r
-  Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2008 - 2011, 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
@@ -23,7 +23,7 @@
 //\r
 // This table defines the offset, base and length of the fixed MTRRs\r
 //\r
-FIXED_MTRR    MtrrLibFixedMtrrTable[] = {\r
+CONST FIXED_MTRR  mMtrrLibFixedMtrrTable[] = {\r
   {\r
     MTRR_LIB_IA32_MTRR_FIX64K_00000,\r
     0,\r
@@ -81,6 +81,20 @@ FIXED_MTRR    MtrrLibFixedMtrrTable[] = {
   },\r
 };\r
 \r
+//\r
+// Lookup table used to print MTRRs\r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mMtrrMemoryCacheTypeShortName[] = {\r
+  "UC",  // CacheUncacheable\r
+  "WC",  // CacheWriteCombining\r
+  "R*",  // Invalid\r
+  "R*",  // Invalid\r
+  "WT",  // CacheWriteThrough\r
+  "WP",  // CacheWriteProtected\r
+  "WB",  // CacheWriteBack\r
+  "R*"   // Invalid\r
+};\r
+\r
 /**\r
   Returns the variable MTRR count for the CPU.\r
 \r
@@ -252,11 +266,11 @@ ProgramFixedMtrr (
   ClearMask = 0;\r
 \r
   for (MsrNum = 0; MsrNum < MTRR_NUMBER_OF_FIXED_MTRR; MsrNum++) {\r
-    if ((*Base >= MtrrLibFixedMtrrTable[MsrNum].BaseAddress) &&\r
+    if ((*Base >= mMtrrLibFixedMtrrTable[MsrNum].BaseAddress) &&\r
         (*Base <\r
             (\r
-              MtrrLibFixedMtrrTable[MsrNum].BaseAddress +\r
-              (8 * MtrrLibFixedMtrrTable[MsrNum].Length)\r
+              mMtrrLibFixedMtrrTable[MsrNum].BaseAddress +\r
+              (8 * mMtrrLibFixedMtrrTable[MsrNum].Length)\r
             )\r
           )\r
         ) {\r
@@ -274,8 +288,8 @@ ProgramFixedMtrr (
   for (ByteShift = 0; ByteShift < 8; ByteShift++) {\r
     if (*Base ==\r
          (\r
-           MtrrLibFixedMtrrTable[MsrNum].BaseAddress +\r
-           (ByteShift * MtrrLibFixedMtrrTable[MsrNum].Length)\r
+           mMtrrLibFixedMtrrTable[MsrNum].BaseAddress +\r
+           (ByteShift * mMtrrLibFixedMtrrTable[MsrNum].Length)\r
          )\r
        ) {\r
       break;\r
@@ -288,13 +302,13 @@ ProgramFixedMtrr (
 \r
   for (\r
         ;\r
-        ((ByteShift < 8) && (*Length >= MtrrLibFixedMtrrTable[MsrNum].Length));\r
+        ((ByteShift < 8) && (*Length >= mMtrrLibFixedMtrrTable[MsrNum].Length));\r
         ByteShift++\r
       ) {\r
     OrMask |= LShiftU64 ((UINT64) MemoryCacheType, (UINT32) (ByteShift * 8));\r
     ClearMask |= LShiftU64 ((UINT64) 0xFF, (UINT32) (ByteShift * 8));\r
-    *Length -= MtrrLibFixedMtrrTable[MsrNum].Length;\r
-    *Base += MtrrLibFixedMtrrTable[MsrNum].Length;\r
+    *Length -= mMtrrLibFixedMtrrTable[MsrNum].Length;\r
+    *Base += mMtrrLibFixedMtrrTable[MsrNum].Length;\r
   }\r
 \r
   if (ByteShift < 8 && (*Length != 0)) {\r
@@ -302,8 +316,8 @@ ProgramFixedMtrr (
   }\r
 \r
   TempQword =\r
-    (AsmReadMsr64 (MtrrLibFixedMtrrTable[MsrNum].Msr) & ~ClearMask) | OrMask;\r
-  AsmWriteMsr64 (MtrrLibFixedMtrrTable[MsrNum].Msr, TempQword);\r
+    (AsmReadMsr64 (mMtrrLibFixedMtrrTable[MsrNum].Msr) & ~ClearMask) | OrMask;\r
+  AsmWriteMsr64 (mMtrrLibFixedMtrrTable[MsrNum].Msr, TempQword);\r
   return RETURN_SUCCESS;\r
 }\r
 \r
@@ -745,8 +759,8 @@ MtrrLibInitializeMtrrMask (
   OUT UINT64 *MtrrValidAddressMask\r
   )\r
 {\r
-  UINT32                              RegEax;\r
-  UINT8                               PhysicalAddressBits;\r
+  UINT32  RegEax;\r
+  UINT8   PhysicalAddressBits;\r
 \r
   AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
 \r
@@ -887,8 +901,11 @@ MtrrSetMemoryAttribute (
   UINT32                    FirmwareVariableMtrrCount;\r
   UINT32                    VariableMtrrEnd;\r
 \r
+  DEBUG((DEBUG_CACHE, "MtrrSetMemoryAttribute() %a:%016lx-%016lx\n", mMtrrMemoryCacheTypeShortName[Attribute], BaseAddress, Length));\r
+\r
   if (!IsMtrrSupported ()) {\r
-    return RETURN_UNSUPPORTED;\r
+    Status = RETURN_UNSUPPORTED;\r
+    goto Done;\r
   }\r
 \r
   FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount ();\r
@@ -904,14 +921,16 @@ MtrrSetMemoryAttribute (
   // Check for an invalid parameter\r
   //\r
   if (Length == 0) {\r
-    return RETURN_INVALID_PARAMETER;\r
+    Status = RETURN_INVALID_PARAMETER;\r
+    goto Done;\r
   }\r
 \r
   if (\r
-       (BaseAddress &~MtrrValidAddressMask) != 0 ||\r
-       (Length &~MtrrValidAddressMask) != 0\r
+       (BaseAddress & ~MtrrValidAddressMask) != 0 ||\r
+       (Length & ~MtrrValidAddressMask) != 0\r
      ) {\r
-    return RETURN_UNSUPPORTED;\r
+    Status = RETURN_UNSUPPORTED;\r
+    goto Done;\r
   }\r
 \r
   //\r
@@ -923,7 +942,7 @@ MtrrSetMemoryAttribute (
     Status = ProgramFixedMtrr (MemoryType, &BaseAddress, &Length);\r
     PostMtrrChange (Cr4);\r
     if (RETURN_ERROR (Status)) {\r
-      return Status;\r
+      goto Done;\r
     }\r
   }\r
 \r
@@ -938,7 +957,7 @@ MtrrSetMemoryAttribute (
 \r
   //\r
   // Since memory ranges below 1MB will be overridden by the fixed MTRRs,\r
-  // we can set the bade to 0 to save variable MTRRs.\r
+  // we can set the base to 0 to save variable MTRRs.\r
   //\r
   if (BaseAddress == BASE_1MB) {\r
     BaseAddress = 0;\r
@@ -1099,8 +1118,12 @@ MtrrSetMemoryAttribute (
   }\r
 \r
 Done:\r
-  return Status;\r
+  DEBUG((DEBUG_CACHE, "  Status = %r\n", Status));\r
+  if (!RETURN_ERROR (Status)) {\r
+    MtrrDebugPrintAllMtrrs ();\r
+  }\r
 \r
+  return Status;\r
 }\r
 \r
 \r
@@ -1154,16 +1177,16 @@ MtrrGetMemoryAttribute (
       // Go through the fixed MTRR\r
       //\r
       for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {\r
-         if (Address >= MtrrLibFixedMtrrTable[Index].BaseAddress &&\r
+         if (Address >= mMtrrLibFixedMtrrTable[Index].BaseAddress &&\r
              Address  < (\r
-                          MtrrLibFixedMtrrTable[Index].BaseAddress +\r
-                          (MtrrLibFixedMtrrTable[Index].Length * 8)\r
+                          mMtrrLibFixedMtrrTable[Index].BaseAddress +\r
+                          (mMtrrLibFixedMtrrTable[Index].Length * 8)\r
                         )\r
             ) {\r
            SubIndex =\r
-             ((UINTN)Address - MtrrLibFixedMtrrTable[Index].BaseAddress) /\r
-               MtrrLibFixedMtrrTable[Index].Length;\r
-           TempQword = AsmReadMsr64 (MtrrLibFixedMtrrTable[Index].Msr);\r
+             ((UINTN)Address - mMtrrLibFixedMtrrTable[Index].BaseAddress) /\r
+               mMtrrLibFixedMtrrTable[Index].Length;\r
+           TempQword = AsmReadMsr64 (mMtrrLibFixedMtrrTable[Index].Msr);\r
            MtrrType =  RShiftU64 (TempQword, SubIndex * 8) & 0xFF;\r
            return GetMemoryCacheTypeFromMtrrType (MtrrType);\r
          }\r
@@ -1312,7 +1335,7 @@ MtrrGetFixedMtrr (
 \r
   for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {\r
       FixedSettings->Mtrr[Index] =\r
-        AsmReadMsr64 (MtrrLibFixedMtrrTable[Index].Msr);\r
+        AsmReadMsr64 (mMtrrLibFixedMtrrTable[Index].Msr);\r
   };\r
 \r
   return FixedSettings;\r
@@ -1333,7 +1356,7 @@ MtrrSetFixedMtrrWorker (
 \r
   for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {\r
      AsmWriteMsr64 (\r
-       MtrrLibFixedMtrrTable[Index].Msr,\r
+       mMtrrLibFixedMtrrTable[Index].Msr,\r
        FixedSettings->Mtrr[Index]\r
        );\r
   }\r
@@ -1447,7 +1470,6 @@ MtrrSetAllMtrrs (
   return MtrrSetting;\r
 }\r
 \r
-\r
 /**\r
   This function prints all MTRRs for debugging.\r
 **/\r
@@ -1461,31 +1483,138 @@ MtrrDebugPrintAllMtrrs (
     {\r
       MTRR_SETTINGS  MtrrSettings;\r
       UINTN          Index;\r
+      UINTN          Index1;\r
       UINTN          VariableMtrrCount;\r
+      UINT64         Base;\r
+      UINT64         Limit;\r
+      UINT64         MtrrBase;\r
+      UINT64         MtrrLimit;\r
+      UINT64         RangeBase;\r
+      UINT64         RangeLimit;\r
+      UINT64         NoRangeBase;\r
+      UINT64         NoRangeLimit;\r
+      UINT32         RegEax;\r
+      UINTN          MemoryType;\r
+      UINTN          PreviousMemoryType;\r
+      BOOLEAN        Found;\r
 \r
       if (!IsMtrrSupported ()) {\r
         return;\r
       }\r
 \r
+      DEBUG((DEBUG_CACHE, "MTRR Settings\n"));\r
+      DEBUG((DEBUG_CACHE, "=============\n"));\r
+      \r
       MtrrGetAllMtrrs (&MtrrSettings);\r
-      DEBUG((EFI_D_ERROR, "DefaultType = %016lx\n", MtrrSettings.MtrrDefType));\r
+      DEBUG((DEBUG_CACHE, "MTRR Default Type: %016lx\n", MtrrSettings.MtrrDefType));\r
       for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {\r
-        DEBUG((\r
-          EFI_D_ERROR, "Fixed[%02d] = %016lx\n",\r
-          Index,\r
-          MtrrSettings.Fixed.Mtrr[Index]\r
-          ));\r
+        DEBUG((DEBUG_CACHE, "Fixed MTRR[%02d]   : %016lx\n", Index, MtrrSettings.Fixed.Mtrr[Index]));\r
       }\r
 \r
       VariableMtrrCount = GetVariableMtrrCount ();\r
       for (Index = 0; Index < VariableMtrrCount; Index++) {\r
-        DEBUG((\r
-          EFI_D_ERROR, "Variable[%02d] = %016lx, %016lx\n",\r
+        DEBUG((DEBUG_CACHE, "Variable MTRR[%02d]: Base=%016lx Mask=%016lx\n",\r
           Index,\r
           MtrrSettings.Variables.Mtrr[Index].Base,\r
           MtrrSettings.Variables.Mtrr[Index].Mask\r
           ));\r
       }\r
+      DEBUG((DEBUG_CACHE, "\n"));\r
+      DEBUG((DEBUG_CACHE, "MTRR Ranges\n"));\r
+      DEBUG((DEBUG_CACHE, "====================================\n"));\r
+\r
+      Base = 0;\r
+      PreviousMemoryType = MTRR_CACHE_INVALID_TYPE;\r
+      for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {\r
+        Base = mMtrrLibFixedMtrrTable[Index].BaseAddress;\r
+        for (Index1 = 0; Index1 < 8; Index1++) {\r
+          MemoryType = RShiftU64 (MtrrSettings.Fixed.Mtrr[Index], Index1 * 8) & 0xff;\r
+          if (MemoryType > CacheWriteBack) {\r
+            MemoryType = MTRR_CACHE_INVALID_TYPE;\r
+          }            \r
+          if (MemoryType != PreviousMemoryType) {\r
+            if (PreviousMemoryType != MTRR_CACHE_INVALID_TYPE) {\r
+              DEBUG((DEBUG_CACHE, "%016lx\n", Base - 1));\r
+            }\r
+            PreviousMemoryType = MemoryType;\r
+            DEBUG((DEBUG_CACHE, "%a:%016lx-", mMtrrMemoryCacheTypeShortName[MemoryType], Base));\r
+          }\r
+          Base += mMtrrLibFixedMtrrTable[Index].Length;\r
+        }\r
+      }\r
+      DEBUG((DEBUG_CACHE, "%016lx\n", Base - 1));\r
+\r
+      VariableMtrrCount = GetVariableMtrrCount ();\r
+\r
+      Base = BASE_1MB;\r
+      PreviousMemoryType = MTRR_CACHE_INVALID_TYPE;\r
+      do {\r
+        MemoryType = MtrrGetMemoryAttribute (Base);\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
+          }\r
+          PreviousMemoryType = MemoryType;\r
+          DEBUG((DEBUG_CACHE, "%a:%016lx-", mMtrrMemoryCacheTypeShortName[MemoryType], Base));\r
+        }\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
+        for (Index = 0, Found = FALSE; Index < VariableMtrrCount; Index++) {\r
+          if ((MtrrSettings.Variables.Mtrr[Index].Mask & BIT11) == 0) {\r
+            //\r
+            // If mask is not valid, then do not display range\r
+            //\r
+            continue;\r
+          }\r
+          MtrrBase  = (MtrrSettings.Variables.Mtrr[Index].Base & (~(SIZE_4KB - 1)));\r
+          MtrrLimit = MtrrBase + ((~(MtrrSettings.Variables.Mtrr[Index].Mask & (~(SIZE_4KB - 1)))) & Limit);\r
+\r
+          if (Base >= MtrrBase && Base < MtrrLimit) {\r
+            Found = TRUE;\r
+          }\r
+          \r
+          if (Base >= MtrrBase && MtrrBase > RangeBase) {\r
+            RangeBase = MtrrBase;\r
+          }\r
+          if (Base > MtrrLimit && MtrrLimit > RangeBase) {\r
+            RangeBase = MtrrLimit + 1;\r
+          }\r
+          if (Base < MtrrBase && MtrrBase < RangeLimit) {\r
+            RangeLimit = MtrrBase - 1;\r
+          }\r
+          if (Base < MtrrLimit && MtrrLimit <= RangeLimit) {\r
+            RangeLimit = MtrrLimit;\r
+          }\r
+          \r
+          if (Base > MtrrLimit && NoRangeBase < MtrrLimit) {\r
+            NoRangeBase = MtrrLimit + 1;\r
+          }\r
+          if (Base < MtrrBase && NoRangeLimit > MtrrBase) {\r
+            NoRangeLimit = MtrrBase - 1;\r
+          }\r
+        }\r
+        \r
+        if (Found) {\r
+          Base = RangeLimit + 1;\r
+        } else {\r
+          Base = NoRangeLimit + 1;\r
+        }\r
+      } while (Found);\r
+      DEBUG((DEBUG_CACHE, "%016lx\n\n", Base - 1));\r
     }\r
   );\r
 }\r