public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement
@ 2023-09-13  4:26 Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 01/16] UefiCpuPkg/MtrrLib: Add internal function MtrrLibIsMtrrSupported Yuanhao Xie
                   ` (17 more replies)
  0 siblings, 18 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel

Update MtrrLib modules to handle the absense of Fixed MTRRs
Update unit test of Update for the absense of Fixed MTRRs

Ray Ni (6):
  UefiCpuPkg/MtrrLib: Add internal function MtrrLibIsMtrrSupported.
  UefiCpuPkg/MtrrUnitTest: Update test to cover no-fixed-mtrr cases.
  UefiCpuPkg/MtrrLib: Fix MtrrGetAllMtrrs to return correct MTRR
    setting.
  UefiCpuPkg/MtrrLib: Fix MtrrSetAllMtrrs to handle absent fixed MTRRs.
  UefiCpuPkg/MtrrLib: Update APIs related to set memory attributes.
  UefiCpuPkg/CpuDxe: Update RefreshMemoryAttributesFromMtrr.

YuanhaoXie (10):
  UefiCpuPkg/MtrrUnitTest: Update the Unit Test for IsMtrrSupported().
  UefiCpuPkg/MtrrUnitTest: Update UnitTestGetFirmwareVariableMtrrCount.
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetDefaultMemoryType.
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetAllMtrrs().
  UefiCpuPkg/MtrrLib: Update MtrrGetFixedMtrr().
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetFixedMtrr().
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrSetAllMtrrs().
  UefiCpuPkg/MtrrLib: Add API MtrrGetMemoryAttributesInMtrrSettings.
  UefiCpuPkg/MtrrLib: Improve MtrrDebugPrintAllMtrrsWorker.
  UefiCpuPkg/MtrrUnitTest: Add Unit test of setting/getting memory
    attributes

 UefiCpuPkg/CpuDxe/CpuDxe.c                            | 309 ++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 UefiCpuPkg/Include/Library/MtrrLib.h                  |  32 +++++++++++++++++++++++++++++---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c                  | 391 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------
 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------
 UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c         |  24 +++++++++++++++---------
 5 files changed, 532 insertions(+), 468 deletions(-)

-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108556): https://edk2.groups.io/g/devel/message/108556
Mute This Topic: https://groups.io/mt/101331015/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 01/16] UefiCpuPkg/MtrrLib: Add internal function MtrrLibIsMtrrSupported.
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 02/16] UefiCpuPkg/MtrrUnitTest: Update the Unit Test for IsMtrrSupported() Yuanhao Xie
                   ` (16 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Eric Dong, Rahul Kumar, Gerd Hoffmann

From: Ray Ni <ray.ni@intel.com>

Add internal function MtrrLibIsMtrrSupported and
update IsMtrrSupported to call the new internal function.

Signed-off-by: Ray Ni <ray.ni@intel.com>

Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 60 insertions(+), 23 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
index 22ec8d2a48..bd61ffc240 100644
--- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
+++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
@@ -5,7 +5,7 @@
     Most of services in this library instance are suggested to be invoked by BSP only,
     except for MtrrSetAllMtrrs() which is used to sync BSP's MTRR setting to APs.
 
-  Copyright (c) 2008 - 2020, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2008 - 2023, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -140,6 +140,64 @@ MtrrDebugPrintAllMtrrsWorker (
   IN MTRR_SETTINGS  *MtrrSetting
   );
 
+/**
+  Return whether MTRR is supported.
+
+  @param[out]  FixedMtrrSupported   Return whether fixed MTRR is supported.
+  @param[out]  VariableMtrrCount    Return the max number of variable MTRRs.
+
+  @retval TRUE  MTRR is supported when either fixed MTRR is supported or max number
+                of variable MTRRs is not 0.
+  @retval FALSE MTRR is not supported when both fixed MTRR is not supported and max
+                number of variable MTRRs is 0.
+**/
+BOOLEAN
+MtrrLibIsMtrrSupported (
+  OUT BOOLEAN  *FixedMtrrSupported  OPTIONAL,
+  OUT UINT32   *VariableMtrrCount   OPTIONAL
+  )
+{
+  CPUID_VERSION_INFO_EDX     Edx;
+  MSR_IA32_MTRRCAP_REGISTER  MtrrCap;
+
+  //
+  // Check CPUID(1).EDX[12] for MTRR capability
+  //
+  AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &Edx.Uint32);
+  if (Edx.Bits.MTRR == 0) {
+    if (FixedMtrrSupported != NULL) {
+      *FixedMtrrSupported = FALSE;
+    }
+
+    if (VariableMtrrCount != NULL) {
+      *VariableMtrrCount = 0;
+    }
+
+    return FALSE;
+  }
+
+  //
+  // Check the number of variable MTRRs and determine whether fixed MTRRs exist.
+  // If the count of variable MTRRs is zero and there are no fixed MTRRs,
+  // then return false
+  //
+  MtrrCap.Uint64 = AsmReadMsr64 (MSR_IA32_MTRRCAP);
+  ASSERT (MtrrCap.Bits.VCNT <= ARRAY_SIZE (((MTRR_VARIABLE_SETTINGS *)0)->Mtrr));
+  if (FixedMtrrSupported != NULL) {
+    *FixedMtrrSupported = (BOOLEAN)(MtrrCap.Bits.FIX == 1);
+  }
+
+  if (VariableMtrrCount != NULL) {
+    *VariableMtrrCount = MtrrCap.Bits.VCNT;
+  }
+
+  if ((MtrrCap.Bits.VCNT == 0) && (MtrrCap.Bits.FIX == 0)) {
+    return FALSE;
+  }
+
+  return TRUE;
+}
+
 /**
   Worker function returns the variable MTRR count for the CPU.
 
@@ -2847,28 +2905,7 @@ IsMtrrSupported (
   VOID
   )
 {
-  CPUID_VERSION_INFO_EDX     Edx;
-  MSR_IA32_MTRRCAP_REGISTER  MtrrCap;
-
-  //
-  // Check CPUID(1).EDX[12] for MTRR capability
-  //
-  AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &Edx.Uint32);
-  if (Edx.Bits.MTRR == 0) {
-    return FALSE;
-  }
-
-  //
-  // Check number of variable MTRRs and fixed MTRRs existence.
-  // If number of variable MTRRs is zero, or fixed MTRRs do not
-  // exist, return false.
-  //
-  MtrrCap.Uint64 = AsmReadMsr64 (MSR_IA32_MTRRCAP);
-  if ((MtrrCap.Bits.VCNT == 0) || (MtrrCap.Bits.FIX == 0)) {
-    return FALSE;
-  }
-
-  return TRUE;
+  return MtrrLibIsMtrrSupported (NULL, NULL);
 }
 
 /**
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108557): https://edk2.groups.io/g/devel/message/108557
Mute This Topic: https://groups.io/mt/101331016/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 02/16] UefiCpuPkg/MtrrUnitTest: Update the Unit Test for IsMtrrSupported().
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 01/16] UefiCpuPkg/MtrrLib: Add internal function MtrrLibIsMtrrSupported Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 03/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestGetFirmwareVariableMtrrCount Yuanhao Xie
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Eric Dong, Rahul Kumar, Gerd Hoffmann

The previous implementation returns FALSE if either fixed MTRR is
unsupported or the number of variable MTRRs is 0. The correct behavior
is to return FALSE only when both fixed MTRR is unsupported and the
number of variable MTRRs is 0.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
index 1409ae27bb..ac18d9d25d 100644
--- a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
+++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
@@ -399,7 +399,7 @@ UnitTestIsMtrrSupported (
   SystemParameter.VariableMtrrCount  = 0;
   SystemParameter.FixedMtrrSupported = TRUE;
   InitializeMtrrRegs (&SystemParameter);
-  UT_ASSERT_FALSE (IsMtrrSupported ());
+  UT_ASSERT_TRUE (IsMtrrSupported ());
 
   //
   // MTRR capability on in CPUID leaf, but no fixed MTRRs.
@@ -408,7 +408,7 @@ UnitTestIsMtrrSupported (
   SystemParameter.VariableMtrrCount  = 7;
   SystemParameter.FixedMtrrSupported = FALSE;
   InitializeMtrrRegs (&SystemParameter);
-  UT_ASSERT_FALSE (IsMtrrSupported ());
+  UT_ASSERT_TRUE (IsMtrrSupported ());
 
   //
   // MTRR capability on in CPUID leaf with both variable and fixed MTRRs.
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108558): https://edk2.groups.io/g/devel/message/108558
Mute This Topic: https://groups.io/mt/101331018/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 03/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestGetFirmwareVariableMtrrCount.
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 01/16] UefiCpuPkg/MtrrLib: Add internal function MtrrLibIsMtrrSupported Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 02/16] UefiCpuPkg/MtrrUnitTest: Update the Unit Test for IsMtrrSupported() Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 04/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetDefaultMemoryType Yuanhao Xie
                   ` (14 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Eric Dong, Rahul Kumar, Gerd Hoffmann

Update UnitTestGetFirmwareVariableMtrrCount for the case the when
Fixed MTRRs are not supported.

The original implementation returns FALSE when either fixed MTRR isn't
supported or the number of variable MTRRs is 0. The correct behavior
should return FALSE only when both fixed MTRR isn't supported and the
number of variable MTRRs is 0.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
index ac18d9d25d..75ae4d65b9 100644
--- a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
+++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
@@ -550,7 +550,7 @@ UnitTestGetFirmwareVariableMtrrCount (
   InitializeMtrrRegs (&SystemParameter);
   PatchPcdSet32 (PcdCpuNumberOfReservedVariableMtrrs, 2);
   Result = GetFirmwareVariableMtrrCount ();
-  UT_ASSERT_EQUAL (Result, 0);
+  UT_ASSERT_EQUAL (Result, SystemParameter.VariableMtrrCount - 2);
 
   //
   // Expect ASSERT() if variable MTRR count is > MTRR_NUMBER_OF_VARIABLE_MTRR
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108559): https://edk2.groups.io/g/devel/message/108559
Mute This Topic: https://groups.io/mt/101331020/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 04/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetDefaultMemoryType.
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (2 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 03/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestGetFirmwareVariableMtrrCount Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 05/16] UefiCpuPkg/MtrrUnitTest: Update test to cover no-fixed-mtrr cases Yuanhao Xie
                   ` (13 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Eric Dong, Rahul Kumar, Gerd Hoffmann

Update UnitTestMtrrGetDefaultMemoryType for the case the when Fixed
MTRRs are not supported.
The original implementation returns FALSE when either fixed MTRR isn't
supported or the number of variable MTRRs is 0. The correct behavior
should return FALSE only when both fixed MTRR isn't supported and the
number of variable MTRRs is 0.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
index 75ae4d65b9..293f3bc7b5 100644
--- a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
+++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
@@ -896,26 +896,18 @@ UnitTestMtrrGetDefaultMemoryType (
     UT_ASSERT_EQUAL (Result, SystemParameter.DefaultCacheType);
   }
 
-  //
-  // If MTRRs are not supported, then always return CacheUncacheable
-  //
-  SystemParameter.MtrrSupported = FALSE;
-  InitializeMtrrRegs (&SystemParameter);
-  Result = MtrrGetDefaultMemoryType ();
-  UT_ASSERT_EQUAL (Result, CacheUncacheable);
-
   SystemParameter.MtrrSupported      = TRUE;
   SystemParameter.FixedMtrrSupported = FALSE;
   InitializeMtrrRegs (&SystemParameter);
   Result = MtrrGetDefaultMemoryType ();
-  UT_ASSERT_EQUAL (Result, CacheUncacheable);
+  UT_ASSERT_EQUAL (Result, SystemParameter.DefaultCacheType);
 
   SystemParameter.MtrrSupported      = TRUE;
   SystemParameter.FixedMtrrSupported = TRUE;
   SystemParameter.VariableMtrrCount  = 0;
   InitializeMtrrRegs (&SystemParameter);
   Result = MtrrGetDefaultMemoryType ();
-  UT_ASSERT_EQUAL (Result, CacheUncacheable);
+  UT_ASSERT_EQUAL (Result, SystemParameter.DefaultCacheType);
 
   return UNIT_TEST_PASSED;
 }
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108560): https://edk2.groups.io/g/devel/message/108560
Mute This Topic: https://groups.io/mt/101331022/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 05/16] UefiCpuPkg/MtrrUnitTest: Update test to cover no-fixed-mtrr cases.
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (3 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 04/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetDefaultMemoryType Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 06/16] UefiCpuPkg/MtrrLib: Fix MtrrGetAllMtrrs to return correct MTRR setting Yuanhao Xie
                   ` (12 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Eric Dong, Rahul Kumar, Gerd Hoffmann

From: Ray Ni <ray.ni@intel.com>

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c | 43 ++++++++++++++++++++++++-------------------
 UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c         | 24 +++++++++++++++---------
 2 files changed, 39 insertions(+), 28 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
index 293f3bc7b5..13cebee32b 100644
--- a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
+++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
@@ -13,25 +13,30 @@ STATIC CONST MTRR_LIB_SYSTEM_PARAMETER  mDefaultSystemParameter = {
 };
 
 STATIC MTRR_LIB_SYSTEM_PARAMETER  mSystemParameters[] = {
-  { 38, TRUE, TRUE, CacheUncacheable,    12 },
-  { 38, TRUE, TRUE, CacheWriteBack,      12 },
-  { 38, TRUE, TRUE, CacheWriteThrough,   12 },
-  { 38, TRUE, TRUE, CacheWriteProtected, 12 },
-  { 38, TRUE, TRUE, CacheWriteCombining, 12 },
-
-  { 42, TRUE, TRUE, CacheUncacheable,    12 },
-  { 42, TRUE, TRUE, CacheWriteBack,      12 },
-  { 42, TRUE, TRUE, CacheWriteThrough,   12 },
-  { 42, TRUE, TRUE, CacheWriteProtected, 12 },
-  { 42, TRUE, TRUE, CacheWriteCombining, 12 },
-
-  { 48, TRUE, TRUE, CacheUncacheable,    12 },
-  { 48, TRUE, TRUE, CacheWriteBack,      12 },
-  { 48, TRUE, TRUE, CacheWriteThrough,   12 },
-  { 48, TRUE, TRUE, CacheWriteProtected, 12 },
-  { 48, TRUE, TRUE, CacheWriteCombining, 12 },
-
-  { 48, TRUE, TRUE, CacheWriteBack,      12, 7}, // 7 bits for MKTME
+  { 38, TRUE, TRUE,  CacheUncacheable,    12 },
+  { 38, TRUE, TRUE,  CacheWriteBack,      12 },
+  { 38, TRUE, TRUE,  CacheWriteThrough,   12 },
+  { 38, TRUE, TRUE,  CacheWriteProtected, 12 },
+  { 38, TRUE, TRUE,  CacheWriteCombining, 12 },
+
+  { 42, TRUE, TRUE,  CacheUncacheable,    12 },
+  { 42, TRUE, TRUE,  CacheWriteBack,      12 },
+  { 42, TRUE, TRUE,  CacheWriteThrough,   12 },
+  { 42, TRUE, TRUE,  CacheWriteProtected, 12 },
+  { 42, TRUE, TRUE,  CacheWriteCombining, 12 },
+
+  { 48, TRUE, TRUE,  CacheUncacheable,    12 },
+  { 48, TRUE, TRUE,  CacheWriteBack,      12 },
+  { 48, TRUE, TRUE,  CacheWriteThrough,   12 },
+  { 48, TRUE, TRUE,  CacheWriteProtected, 12 },
+  { 48, TRUE, TRUE,  CacheWriteCombining, 12 },
+
+  { 48, TRUE, FALSE, CacheUncacheable,    12 },
+  { 48, TRUE, FALSE, CacheWriteBack,      12 },
+  { 48, TRUE, FALSE, CacheWriteThrough,   12 },
+  { 48, TRUE, FALSE, CacheWriteProtected, 12 },
+  { 48, TRUE, FALSE, CacheWriteCombining, 12 },
+  { 48, TRUE, TRUE,  CacheWriteBack,      12, 7},  // 7 bits for MKTME
 };
 
 UINT32  mFixedMtrrsIndex[] = {
diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c b/UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c
index ba1de10034..7df5b9745f 100644
--- a/UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c
+++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c
@@ -43,7 +43,6 @@ Rand (
   if (mRandomInput) {
     return rand ();
   } else {
-    DEBUG ((DEBUG_INFO, "random: %d\n", mNumberIndex));
     return mNumbers[mNumberIndex++ % (mNumberCount - 1)];
   }
 }
@@ -236,8 +235,11 @@ UnitTestMtrrLibAsmReadMsr64 (
 {
   UINT32  Index;
 
+  UT_ASSERT_EQUAL (mCpuidVersionInfoEdx.Bits.MTRR, 1);
+
   for (Index = 0; Index < ARRAY_SIZE (mFixedMtrrsValue); Index++) {
     if (MsrIndex == mFixedMtrrsIndex[Index]) {
+      UT_ASSERT_EQUAL (mMtrrCapMsr.Bits.FIX, 1);
       return mFixedMtrrsValue[Index];
     }
   }
@@ -245,6 +247,7 @@ UnitTestMtrrLibAsmReadMsr64 (
   if ((MsrIndex >= MSR_IA32_MTRR_PHYSBASE0) &&
       (MsrIndex <= MSR_IA32_MTRR_PHYSMASK0 + (MTRR_NUMBER_OF_VARIABLE_MTRR << 1)))
   {
+    UT_ASSERT_TRUE (((MsrIndex - MSR_IA32_MTRR_PHYSBASE0) >> 1) < mMtrrCapMsr.Bits.VCNT);
     if (MsrIndex % 2 == 0) {
       Index = (MsrIndex - MSR_IA32_MTRR_PHYSBASE0) >> 1;
       return mVariableMtrrsPhysBase[Index].Uint64;
@@ -299,8 +302,11 @@ UnitTestMtrrLibAsmWriteMsr64 (
 {
   UINT32  Index;
 
+  UT_ASSERT_EQUAL (mCpuidVersionInfoEdx.Bits.MTRR, 1);
+
   for (Index = 0; Index < ARRAY_SIZE (mFixedMtrrsValue); Index++) {
     if (MsrIndex == mFixedMtrrsIndex[Index]) {
+      UT_ASSERT_EQUAL (mMtrrCapMsr.Bits.FIX, 1);
       mFixedMtrrsValue[Index] = Value;
       return Value;
     }
@@ -309,6 +315,7 @@ UnitTestMtrrLibAsmWriteMsr64 (
   if ((MsrIndex >= MSR_IA32_MTRR_PHYSBASE0) &&
       (MsrIndex <= MSR_IA32_MTRR_PHYSMASK0 + (MTRR_NUMBER_OF_VARIABLE_MTRR << 1)))
   {
+    UT_ASSERT_TRUE (((MsrIndex - MSR_IA32_MTRR_PHYSBASE0) >> 1) < mMtrrCapMsr.Bits.VCNT);
     if (MsrIndex % 2 == 0) {
       Index                                = (MsrIndex - MSR_IA32_MTRR_PHYSBASE0) >> 1;
       mVariableMtrrsPhysBase[Index].Uint64 = Value;
@@ -321,6 +328,10 @@ UnitTestMtrrLibAsmWriteMsr64 (
   }
 
   if (MsrIndex == MSR_IA32_MTRR_DEF_TYPE) {
+    if (((MSR_IA32_MTRR_DEF_TYPE_REGISTER *)&Value)->Bits.FE == 1) {
+      UT_ASSERT_EQUAL (mMtrrCapMsr.Bits.FIX, 1);
+    }
+
     mDefTypeMsr.Uint64 = Value;
     return Value;
   }
@@ -353,17 +364,12 @@ InitializeMtrrRegs (
   SetMem (mFixedMtrrsValue, sizeof (mFixedMtrrsValue), SystemParameter->DefaultCacheType);
 
   for (Index = 0; Index < ARRAY_SIZE (mVariableMtrrsPhysBase); Index++) {
-    mVariableMtrrsPhysBase[Index].Uint64         = 0;
-    mVariableMtrrsPhysBase[Index].Bits.Type      = SystemParameter->DefaultCacheType;
-    mVariableMtrrsPhysBase[Index].Bits.Reserved1 = 0;
-
-    mVariableMtrrsPhysMask[Index].Uint64         = 0;
-    mVariableMtrrsPhysMask[Index].Bits.V         = 0;
-    mVariableMtrrsPhysMask[Index].Bits.Reserved1 = 0;
+    mVariableMtrrsPhysBase[Index].Uint64 = 0;
+    mVariableMtrrsPhysMask[Index].Uint64 = 0;
   }
 
   mDefTypeMsr.Bits.E         = 1;
-  mDefTypeMsr.Bits.FE        = 1;
+  mDefTypeMsr.Bits.FE        = 0;
   mDefTypeMsr.Bits.Type      = SystemParameter->DefaultCacheType;
   mDefTypeMsr.Bits.Reserved1 = 0;
   mDefTypeMsr.Bits.Reserved2 = 0;
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108561): https://edk2.groups.io/g/devel/message/108561
Mute This Topic: https://groups.io/mt/101331023/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 06/16] UefiCpuPkg/MtrrLib: Fix MtrrGetAllMtrrs to return correct MTRR setting.
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (4 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 05/16] UefiCpuPkg/MtrrUnitTest: Update test to cover no-fixed-mtrr cases Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 07/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetAllMtrrs() Yuanhao Xie
                   ` (11 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Yuanhao Xie, Eric Dong, Rahul Kumar, Gerd Hoffmann

From: Ray Ni <ray.ni@intel.com>

The patch fixes the following issues in the original implementation:

1. MtrrSetting contains random value if MTRR is not supported.
2. Unconditionally access fixed MTRR on CPU that may not support
    fixed MTRR.
3. The maximum number of Variable MTRR entries are initialized, while
the portion exceeding the maximum number remains uninitialized.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>

Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
index bd61ffc240..c9440f01ef 100644
--- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
+++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
@@ -2824,29 +2824,43 @@ MtrrGetAllMtrrs (
   OUT MTRR_SETTINGS  *MtrrSetting
   )
 {
-  if (!IsMtrrSupported ()) {
+  BOOLEAN                          FixedMtrrSupported;
+  UINT32                           VariableMtrrCount;
+  MSR_IA32_MTRR_DEF_TYPE_REGISTER  *MtrrDefType;
+
+  ZeroMem (MtrrSetting, sizeof (*MtrrSetting));
+
+  MtrrDefType = (MSR_IA32_MTRR_DEF_TYPE_REGISTER *)&MtrrSetting->MtrrDefType;
+  if (!MtrrLibIsMtrrSupported (&FixedMtrrSupported, &VariableMtrrCount)) {
     return MtrrSetting;
   }
 
+  //
+  // Get MTRR_DEF_TYPE value
+  //
+  MtrrDefType->Uint64 = AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE);
+
+  //
+  // Enabling the Fixed MTRR bit when unsupported is not allowed.
+  //
+  ASSERT (FixedMtrrSupported || (MtrrDefType->Bits.FE == 0));
+
   //
   // Get fixed MTRRs
   //
-  MtrrGetFixedMtrrWorker (&MtrrSetting->Fixed);
+  if (MtrrDefType->Bits.FE == 1) {
+    MtrrGetFixedMtrrWorker (&MtrrSetting->Fixed);
+  }
 
   //
   // Get variable MTRRs
   //
   MtrrGetVariableMtrrWorker (
     NULL,
-    GetVariableMtrrCountWorker (),
+    VariableMtrrCount,
     &MtrrSetting->Variables
     );
 
-  //
-  // Get MTRR_DEF_TYPE value
-  //
-  MtrrSetting->MtrrDefType = AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE);
-
   return MtrrSetting;
 }
 
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108562): https://edk2.groups.io/g/devel/message/108562
Mute This Topic: https://groups.io/mt/101331024/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 07/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetAllMtrrs().
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (5 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 06/16] UefiCpuPkg/MtrrLib: Fix MtrrGetAllMtrrs to return correct MTRR setting Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 08/16] UefiCpuPkg/MtrrLib: Update MtrrGetFixedMtrr() Yuanhao Xie
                   ` (10 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Yuanhao Xie, Eric Dong, Rahul Kumar, Gerd Hoffmann, Ray Ni

Update UnitTestMtrrGetAllMtrrs() for the case that fixed MtrrLib
is not always supported

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Ray Ni <ray.ni@intel.com>
---
 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 84 insertions(+), 15 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
index 13cebee32b..de9db4fcb4 100644
--- a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
+++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
@@ -653,6 +653,52 @@ UnitTestMtrrGetFixedMtrr (
   return UNIT_TEST_PASSED;
 }
 
+/**
+  Set Random Variable and Fixed MTRRs Settings for
+  unit test of UnitTestMtrrGetAllMtrrs.
+
+  @param SystemParameter      System parameter that controls the MTRR registers initialization.
+  @param ExpectedMtrrs        Expected Fixed and Variable MTRRs.
+**/
+VOID
+SetRandomlyGeneratedMtrrSettings (
+  IN MTRR_LIB_SYSTEM_PARAMETER  *SystemParameter,
+  IN MTRR_SETTINGS              *ExpectedMtrrs
+  )
+{
+  UINT32                           Index;
+  UINTN                            MsrIndex;
+  UINTN                            ByteIndex;
+  UINT64                           MsrValue;
+  MSR_IA32_MTRR_DEF_TYPE_REGISTER  Default;
+
+  AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE, ExpectedMtrrs->MtrrDefType);
+  //
+  // Randomly generate Variable MTRR BASE/MASK for a specified type and write to MSR.
+  //
+  for (Index = 0; Index < SystemParameter->VariableMtrrCount; Index++) {
+    GenerateRandomMtrrPair (SystemParameter->PhysicalAddressBits, GenerateRandomCacheType (), &ExpectedMtrrs->Variables.Mtrr[Index], NULL);
+    AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1), ExpectedMtrrs->Variables.Mtrr[Index].Base);
+    AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1), ExpectedMtrrs->Variables.Mtrr[Index].Mask);
+  }
+
+  //
+  // Set Fixed MTRRs when the Fixed MTRRs is enabled and the MTRRs is supported.
+  //
+  Default.Uint64 = AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE);
+  if ((Default.Bits.FE == 1) && (SystemParameter->MtrrSupported == TRUE)) {
+    for (MsrIndex = 0; MsrIndex < ARRAY_SIZE (mFixedMtrrsIndex); MsrIndex++) {
+      MsrValue = 0;
+      for (ByteIndex = 0; ByteIndex < sizeof (UINT64); ByteIndex++) {
+        MsrValue = MsrValue | LShiftU64 (GenerateRandomCacheType (), ByteIndex * 8);
+      }
+
+      ExpectedMtrrs->Fixed.Mtrr[MsrIndex] = MsrValue;
+      AsmWriteMsr64 (mFixedMtrrsIndex[MsrIndex], MsrValue);
+    }
+  }
+}
+
 /**
   Unit test of MtrrLib service MtrrGetAllMtrrs()
 
@@ -669,28 +715,51 @@ UnitTestMtrrGetAllMtrrs (
   IN UNIT_TEST_CONTEXT  Context
   )
 {
-  MTRR_SETTINGS              *Result;
-  MTRR_SETTINGS              Mtrrs;
-  MTRR_SETTINGS              ExpectedMtrrs;
-  MTRR_VARIABLE_SETTING      VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];
-  UINT32                     Index;
-  MTRR_LIB_SYSTEM_PARAMETER  SystemParameter;
-  MTRR_LIB_TEST_CONTEXT      *LocalContext;
+  MTRR_SETTINGS                    *Result;
+  MTRR_SETTINGS                    Mtrrs;
+  MTRR_SETTINGS                    ExpectedMtrrs;
+  MTRR_LIB_SYSTEM_PARAMETER        SystemParameter;
+  MTRR_LIB_TEST_CONTEXT            *LocalContext;
+  MSR_IA32_MTRR_DEF_TYPE_REGISTER  Default;
 
   LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
 
   CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
-  InitializeMtrrRegs (&SystemParameter);
 
-  for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
-    GenerateRandomMtrrPair (SystemParameter.PhysicalAddressBits, GenerateRandomCacheType (), &VariableMtrr[Index], NULL);
-    AsmWriteMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1), VariableMtrr[Index].Base);
-    AsmWriteMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1), VariableMtrr[Index].Mask);
-  }
+  //
+  // For the case that Fixed MTRRs is NOT enabled
+  //
+  SystemParameter.MtrrSupported      = TRUE;
+  SystemParameter.FixedMtrrSupported = FALSE;
+  InitializeMtrrRegs (&SystemParameter);
+  Default.Uint64  = 0;
+  Default.Bits.E  = 1;
+  Default.Bits.FE = 0;
+  ZeroMem (&ExpectedMtrrs, sizeof (ExpectedMtrrs));
+  ExpectedMtrrs.MtrrDefType = Default.Uint64;
+  //
+  // Randomly generate expected MtrrSettings and set to MSR.
+  //
+  SetRandomlyGeneratedMtrrSettings (&SystemParameter, &ExpectedMtrrs);
+  Result = MtrrGetAllMtrrs (&Mtrrs);
+  UT_ASSERT_MEM_EQUAL (&ExpectedMtrrs.Fixed, &Mtrrs.Fixed, sizeof (MTRR_FIXED_SETTINGS));
+  UT_ASSERT_MEM_EQUAL (Mtrrs.Variables.Mtrr, ExpectedMtrrs.Variables.Mtrr, sizeof (MTRR_VARIABLE_SETTING) * (SystemParameter.VariableMtrrCount));
 
+  //
+  // For the case that Fixed MTRRs is enabled
+  //
+  SystemParameter.MtrrSupported      = TRUE;
+  SystemParameter.FixedMtrrSupported = TRUE;
+  InitializeMtrrRegs (&SystemParameter);
+  Default.Uint64  = 0;
+  Default.Bits.E  = 1;
+  Default.Bits.FE = 1;
+  ZeroMem (&ExpectedMtrrs, sizeof (ExpectedMtrrs));
+  ExpectedMtrrs.MtrrDefType = Default.Uint64;
+  SetRandomlyGeneratedMtrrSettings (&SystemParameter, &ExpectedMtrrs);
   Result = MtrrGetAllMtrrs (&Mtrrs);
-  UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&Mtrrs);
-  UT_ASSERT_MEM_EQUAL (Mtrrs.Variables.Mtrr, VariableMtrr, sizeof (MTRR_VARIABLE_SETTING) * SystemParameter.VariableMtrrCount);
+  UT_ASSERT_MEM_EQUAL (&ExpectedMtrrs.Fixed, &Mtrrs.Fixed, sizeof (MTRR_FIXED_SETTINGS));
+  UT_ASSERT_MEM_EQUAL (Mtrrs.Variables.Mtrr, ExpectedMtrrs.Variables.Mtrr, sizeof (MTRR_VARIABLE_SETTING) * (SystemParameter.VariableMtrrCount));
 
   //
   // Negative test case when MTRRs are not supported
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108563): https://edk2.groups.io/g/devel/message/108563
Mute This Topic: https://groups.io/mt/101331025/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 08/16] UefiCpuPkg/MtrrLib: Update MtrrGetFixedMtrr().
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (6 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 07/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetAllMtrrs() Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 09/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetFixedMtrr() Yuanhao Xie
                   ` (9 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Yuanhao Xie, Eric Dong, Rahul Kumar, Gerd Hoffmann, Ray Ni

IsMtrrSupported() return true when either fixed mtrr supported or
variable mtrr suppored. In this case, rather than checking whether
MTRR is supported or not, we should specifically check for fixed MTRR
support.

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Ray Ni <ray.ni@intel.com>
---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
index c9440f01ef..b63c4b1c26 100644
--- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
+++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
@@ -468,7 +468,10 @@ MtrrGetFixedMtrr (
   OUT MTRR_FIXED_SETTINGS  *FixedSettings
   )
 {
-  if (!IsMtrrSupported ()) {
+  BOOLEAN  FixedMtrrSupported;
+
+  MtrrLibIsMtrrSupported (&FixedMtrrSupported, NULL);
+  if (!FixedMtrrSupported) {
     return FixedSettings;
   }
 
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108564): https://edk2.groups.io/g/devel/message/108564
Mute This Topic: https://groups.io/mt/101331026/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 09/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetFixedMtrr().
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (7 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 08/16] UefiCpuPkg/MtrrLib: Update MtrrGetFixedMtrr() Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 10/16] UefiCpuPkg/MtrrLib: Fix MtrrSetAllMtrrs to handle absent fixed MTRRs Yuanhao Xie
                   ` (8 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Yuanhao Xie, Eric Dong, Rahul Kumar, Gerd Hoffmann, Ray Ni

UnitTestMtrrGetFixedMtrr updated for the case that fixed MtrrLib
is not always supported.

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Ray Ni <ray.ni@intel.com>
---
 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
index de9db4fcb4..2905b091a5 100644
--- a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
+++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
@@ -650,6 +650,19 @@ UnitTestMtrrGetFixedMtrr (
   UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&FixedSettings);
   UT_ASSERT_MEM_EQUAL (&ExpectedFixedSettings, &FixedSettings, sizeof (ExpectedFixedSettings));
 
+  //
+  // Negative test case when Fixed MTRRs are not supported
+  //
+  SystemParameter.MtrrSupported      = TRUE;
+  SystemParameter.FixedMtrrSupported = FALSE;
+  InitializeMtrrRegs (&SystemParameter);
+
+  ZeroMem (&FixedSettings, sizeof (FixedSettings));
+  ZeroMem (&ExpectedFixedSettings, sizeof (ExpectedFixedSettings));
+  Result = MtrrGetFixedMtrr (&FixedSettings);
+  UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&FixedSettings);
+  UT_ASSERT_MEM_EQUAL (&ExpectedFixedSettings, &FixedSettings, sizeof (ExpectedFixedSettings));
+
   return UNIT_TEST_PASSED;
 }
 
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108565): https://edk2.groups.io/g/devel/message/108565
Mute This Topic: https://groups.io/mt/101331027/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 10/16] UefiCpuPkg/MtrrLib: Fix MtrrSetAllMtrrs to handle absent fixed MTRRs.
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (8 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 09/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetFixedMtrr() Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 11/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrSetAllMtrrs() Yuanhao Xie
                   ` (7 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Yuanhao Xie, Eric Dong, Rahul Kumar, Gerd Hoffmann

From: Ray Ni <ray.ni@intel.com>

Update MtrrSetAllMtrrs to not access fixed MTRRs if CPU doesn't
support them.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>

Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Include/Library/MtrrLib.h |  9 ++++++---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 26 ++++++++++++++++++++------
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/UefiCpuPkg/Include/Library/MtrrLib.h b/UefiCpuPkg/Include/Library/MtrrLib.h
index 683d9f2c14..94cf615901 100644
--- a/UefiCpuPkg/Include/Library/MtrrLib.h
+++ b/UefiCpuPkg/Include/Library/MtrrLib.h
@@ -1,7 +1,7 @@
 /** @file
   MTRR setting library
 
-  Copyright (c) 2008 - 2020, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2008 - 2023, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -214,9 +214,12 @@ MtrrGetAllMtrrs (
 /**
   This function sets all MTRRs (variable and fixed)
 
-  @param[in]  MtrrSetting   A buffer to hold all MTRRs content.
+  Note: The behavior of this function is to program everything in MtrrSetting to hardware.
+        MTRR might not be enabled due to enable bit is clear in MtrrSetting->MtrrDefType.
 
-  @return The pointer of MtrrSetting
+  @param[in]  MtrrSetting  A buffer holding all MTRRs content.
+
+  @retval The pointer of MtrrSetting
 
 **/
 MTRR_SETTINGS *
diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
index b63c4b1c26..9a01f1417f 100644
--- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
+++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
@@ -2868,7 +2868,10 @@ MtrrGetAllMtrrs (
 }
 
 /**
-  This function sets all MTRRs (variable and fixed)
+  This function sets all MTRRs includes Variable and Fixed.
+
+  The behavior of this function is to program everything in MtrrSetting to hardware.
+  MTRRs might not be enabled because the enable bit is clear in MtrrSetting->MtrrDefType.
 
   @param[in]  MtrrSetting  A buffer holding all MTRRs content.
 
@@ -2881,21 +2884,32 @@ MtrrSetAllMtrrs (
   IN MTRR_SETTINGS  *MtrrSetting
   )
 {
-  MTRR_CONTEXT  MtrrContext;
+  BOOLEAN                          FixedMtrrSupported;
+  MSR_IA32_MTRR_DEF_TYPE_REGISTER  *MtrrDefType;
+  MTRR_CONTEXT                     MtrrContext;
 
-  if (!IsMtrrSupported ()) {
+  MtrrDefType = (MSR_IA32_MTRR_DEF_TYPE_REGISTER *)&MtrrSetting->MtrrDefType;
+  if (!MtrrLibIsMtrrSupported (&FixedMtrrSupported, NULL)) {
     return MtrrSetting;
   }
 
   MtrrLibPreMtrrChange (&MtrrContext);
 
   //
-  // Set fixed MTRRs
+  // Enabling the Fixed MTRR bit when unsupported is not allowed.
+  //
+  ASSERT (FixedMtrrSupported || (MtrrDefType->Bits.FE == 0));
+
+  //
+  // If the hardware supports Fixed MTRR, it is sufficient
+  // to set MTRRs regardless of whether Fixed MTRR bit is enabled.
   //
-  MtrrSetFixedMtrrWorker (&MtrrSetting->Fixed);
+  if (FixedMtrrSupported) {
+    MtrrSetFixedMtrrWorker (&MtrrSetting->Fixed);
+  }
 
   //
-  // Set variable MTRRs
+  // Set Variable MTRRs
   //
   MtrrSetVariableMtrrWorker (&MtrrSetting->Variables);
 
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108566): https://edk2.groups.io/g/devel/message/108566
Mute This Topic: https://groups.io/mt/101331028/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 11/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrSetAllMtrrs().
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (9 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 10/16] UefiCpuPkg/MtrrLib: Fix MtrrSetAllMtrrs to handle absent fixed MTRRs Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 12/16] UefiCpuPkg/MtrrLib: Update APIs related to set memory attributes Yuanhao Xie
                   ` (6 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Yuanhao Xie, Eric Dong, Rahul Kumar, Gerd Hoffmann, Ray Ni

Update UnitTestMtrrSetAllMtrrs() for the case that fixed MtrrLib
is not always supported.

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Ray Ni <ray.ni@intel.com>
---
 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
index 2905b091a5..dc348ef0a3 100644
--- a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
+++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
@@ -799,13 +799,10 @@ UnitTestMtrrGetAllMtrrs (
 
 /**
   Unit test of MtrrLib service MtrrSetAllMtrrs()
-
   @param[in]  Context    Ignored
-
   @retval  UNIT_TEST_PASSED             The Unit test has completed and the test
                                         case was successful.
   @retval  UNIT_TEST_ERROR_TEST_FAILED  A test case assertion has failed.
-
 **/
 UNIT_TEST_STATUS
 EFIAPI
@@ -814,35 +811,43 @@ UnitTestMtrrSetAllMtrrs (
   )
 {
   MTRR_SETTINGS                    *Result;
-  MTRR_SETTINGS                    Mtrrs;
+  MTRR_SETTINGS                    ExpectedMtrrs;
   UINT32                           Index;
   MSR_IA32_MTRR_DEF_TYPE_REGISTER  Default;
   MTRR_LIB_SYSTEM_PARAMETER        SystemParameter;
   MTRR_LIB_TEST_CONTEXT            *LocalContext;
+  UINTN                            MsrIndex;
+  UINTN                            ByteIndex;
+  UINT64                           MsrValue;
 
   LocalContext = (MTRR_LIB_TEST_CONTEXT *)Context;
-
   CopyMem (&SystemParameter, LocalContext->SystemParameter, sizeof (SystemParameter));
   InitializeMtrrRegs (&SystemParameter);
-
   Default.Uint64    = 0;
   Default.Bits.E    = 1;
   Default.Bits.FE   = 1;
   Default.Bits.Type = GenerateRandomCacheType ();
-
-  ZeroMem (&Mtrrs, sizeof (Mtrrs));
-  Mtrrs.MtrrDefType = Default.Uint64;
+  ZeroMem (&ExpectedMtrrs, sizeof (ExpectedMtrrs));
+  ExpectedMtrrs.MtrrDefType = Default.Uint64;
   for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
-    GenerateRandomMtrrPair (SystemParameter.PhysicalAddressBits, GenerateRandomCacheType (), &Mtrrs.Variables.Mtrr[Index], NULL);
+    GenerateRandomMtrrPair (SystemParameter.PhysicalAddressBits, GenerateRandomCacheType (), &ExpectedMtrrs.Variables.Mtrr[Index], NULL);
   }
 
-  Result = MtrrSetAllMtrrs (&Mtrrs);
-  UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&Mtrrs);
+  for (MsrIndex = 0; MsrIndex < ARRAY_SIZE (mFixedMtrrsIndex); MsrIndex++) {
+    MsrValue = 0;
+    for (ByteIndex = 0; ByteIndex < sizeof (UINT64); ByteIndex++) {
+      MsrValue = MsrValue | LShiftU64 (GenerateRandomCacheType (), ByteIndex * 8);
+    }
+
+    ExpectedMtrrs.Fixed.Mtrr[MsrIndex] = MsrValue;
+  }
 
-  UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE), Mtrrs.MtrrDefType);
+  Result = MtrrSetAllMtrrs (&ExpectedMtrrs);
+  UT_ASSERT_EQUAL ((UINTN)Result, (UINTN)&ExpectedMtrrs);
+  UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE), ExpectedMtrrs.MtrrDefType);
   for (Index = 0; Index < SystemParameter.VariableMtrrCount; Index++) {
-    UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1)), Mtrrs.Variables.Mtrr[Index].Base);
-    UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1)), Mtrrs.Variables.Mtrr[Index].Mask);
+    UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSBASE0 + (Index << 1)), ExpectedMtrrs.Variables.Mtrr[Index].Base);
+    UT_ASSERT_EQUAL (AsmReadMsr64 (MSR_IA32_MTRR_PHYSMASK0 + (Index << 1)), ExpectedMtrrs.Variables.Mtrr[Index].Mask);
   }
 
   return UNIT_TEST_PASSED;
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108567): https://edk2.groups.io/g/devel/message/108567
Mute This Topic: https://groups.io/mt/101331029/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 12/16] UefiCpuPkg/MtrrLib: Update APIs related to set memory attributes.
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (10 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 11/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrSetAllMtrrs() Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 13/16] UefiCpuPkg/MtrrLib: Add API MtrrGetMemoryAttributesInMtrrSettings Yuanhao Xie
                   ` (5 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Eric Dong, Rahul Kumar, Gerd Hoffmann

From: Ray Ni <ray.ni@intel.com>

Update APIs related to set memory attributes to handle the fixed MTRR
is not always supported.

There are 3 APIs in MtrrLib that can set memory attributes:
1. MtrrSetMemoryAttributesInMtrrSettings
2. MtrrSetMemoryAttributeInMtrrSettings
3. MtrrSetMemoryAttribute

The general idea applied in MtrrSetMemoryAttributesInMtrrSettings is:
1. MtrrLibPreMtrrChange saves the old MTRR default type which
   contains bit to enable fixed MTRR.
2. Main logic in MtrrSetMemoryAttributesInMtrrSettings applies
    memory attribute settings for below 1MB to variable MTRRs
    if fixed MTRR is not supported.
3. MtrrLibPostMtrrChange unconditionally sets E bit in MTRR default
    type MSR but only set FE bit when fixed MTRRs are modified.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------
 1 file changed, 56 insertions(+), 39 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
index 9a01f1417f..6ba0c9b37f 100644
--- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
+++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
@@ -33,8 +33,9 @@
 // Context to save and restore when MTRRs are programmed
 //
 typedef struct {
-  UINTN      Cr4;
-  BOOLEAN    InterruptState;
+  UINTN                              Cr4;
+  BOOLEAN                            InterruptState;
+  MSR_IA32_MTRR_DEF_TYPE_REGISTER    DefType;
 } MTRR_CONTEXT;
 
 typedef struct {
@@ -362,10 +363,11 @@ MtrrLibPreMtrrChange (
   CpuFlushTlb ();
 
   //
-  // Disable MTRRs
+  // Save current MTRR default type and disable MTRRs
   //
-  DefType.Uint64 = AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE);
-  DefType.Bits.E = 0;
+  MtrrContext->DefType.Uint64 = AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE);
+  DefType.Uint64              = MtrrContext->DefType.Uint64;
+  DefType.Bits.E              = 0;
   AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE, DefType.Uint64);
 }
 
@@ -418,15 +420,13 @@ MtrrLibPostMtrrChange (
   IN MTRR_CONTEXT  *MtrrContext
   )
 {
-  MSR_IA32_MTRR_DEF_TYPE_REGISTER  DefType;
-
   //
   // Enable Cache MTRR
+  // Note: It's possible that MTRR was not enabled earlier.
+  //       But it will be enabled here unconditionally.
   //
-  DefType.Uint64  = AsmReadMsr64 (MSR_IA32_MTRR_DEF_TYPE);
-  DefType.Bits.E  = 1;
-  DefType.Bits.FE = 1;
-  AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE, DefType.Uint64);
+  MtrrContext->DefType.Bits.E = 1;
+  AsmWriteMsr64 (MSR_IA32_MTRR_DEF_TYPE, MtrrContext->DefType.Uint64);
 
   MtrrLibPostMtrrChangeEnableCache (MtrrContext);
 }
@@ -1057,6 +1057,8 @@ MtrrLibSetMemoryType (
   UINTN   EndIndex;
   UINTN   DeltaCount;
 
+  ASSERT (Length != 0);
+
   LengthRight = 0;
   LengthLeft  = 0;
   Limit       = BaseAddress + Length;
@@ -2335,7 +2337,7 @@ MtrrSetMemoryAttributesInMtrrSettings (
   UINT32         Index;
   UINT64         BaseAddress;
   UINT64         Length;
-  BOOLEAN        Above1MbExist;
+  BOOLEAN        VariableMtrrNeeded;
 
   UINT64                  MtrrValidBitsMask;
   UINT64                  MtrrValidAddressMask;
@@ -2352,8 +2354,10 @@ MtrrSetMemoryAttributesInMtrrSettings (
   MTRR_MEMORY_RANGE       WorkingVariableMtrr[ARRAY_SIZE (MtrrSetting->Variables.Mtrr)];
   BOOLEAN                 VariableSettingModified[ARRAY_SIZE (MtrrSetting->Variables.Mtrr)];
 
-  UINT64  ClearMasks[ARRAY_SIZE (mMtrrLibFixedMtrrTable)];
-  UINT64  OrMasks[ARRAY_SIZE (mMtrrLibFixedMtrrTable)];
+  UINT64   FixedMtrrMemoryLimit;
+  BOOLEAN  FixedMtrrSupported;
+  UINT64   ClearMasks[ARRAY_SIZE (mMtrrLibFixedMtrrTable)];
+  UINT64   OrMasks[ARRAY_SIZE (mMtrrLibFixedMtrrTable)];
 
   MTRR_CONTEXT  MtrrContext;
   BOOLEAN       MtrrContextValid;
@@ -2369,7 +2373,7 @@ MtrrSetMemoryAttributesInMtrrSettings (
   //
   // TRUE indicating the caller requests to set variable MTRRs.
   //
-  Above1MbExist             = FALSE;
+  VariableMtrrNeeded        = FALSE;
   OriginalVariableMtrrCount = 0;
 
   //
@@ -2398,11 +2402,13 @@ MtrrSetMemoryAttributesInMtrrSettings (
   //
   // 1. Validate the parameters.
   //
-  if (!IsMtrrSupported ()) {
+  if (!MtrrLibIsMtrrSupported (&FixedMtrrSupported, &OriginalVariableMtrrCount)) {
     Status = RETURN_UNSUPPORTED;
     goto Exit;
   }
 
+  FixedMtrrMemoryLimit = FixedMtrrSupported ? BASE_1MB : 0;
+
   for (Index = 0; Index < RangeCount; Index++) {
     if (Ranges[Index].Length == 0) {
       Status = RETURN_INVALID_PARAMETER;
@@ -2432,19 +2438,18 @@ MtrrSetMemoryAttributesInMtrrSettings (
       goto Exit;
     }
 
-    if (Ranges[Index].BaseAddress + Ranges[Index].Length > BASE_1MB) {
-      Above1MbExist = TRUE;
+    if (Ranges[Index].BaseAddress + Ranges[Index].Length > FixedMtrrMemoryLimit) {
+      VariableMtrrNeeded = TRUE;
     }
   }
 
   //
   // 2. Apply the above-1MB memory attribute settings.
   //
-  if (Above1MbExist) {
+  if (VariableMtrrNeeded) {
     //
     // 2.1. Read all variable MTRRs and convert to Ranges.
     //
-    OriginalVariableMtrrCount = GetVariableMtrrCountWorker ();
     MtrrGetVariableMtrrWorker (MtrrSetting, OriginalVariableMtrrCount, &VariableSettings);
     MtrrLibGetRawVariableRanges (
       &VariableSettings,
@@ -2476,15 +2481,17 @@ MtrrSetMemoryAttributesInMtrrSettings (
     //
     // 2.2. Force [0, 1M) to UC, so that it doesn't impact subtraction algorithm.
     //
-    Status = MtrrLibSetMemoryType (
-               WorkingRanges,
-               ARRAY_SIZE (WorkingRanges),
-               &WorkingRangeCount,
-               0,
-               SIZE_1MB,
-               CacheUncacheable
-               );
-    ASSERT (Status != RETURN_OUT_OF_RESOURCES);
+    if (FixedMtrrMemoryLimit != 0) {
+      Status = MtrrLibSetMemoryType (
+                 WorkingRanges,
+                 ARRAY_SIZE (WorkingRanges),
+                 &WorkingRangeCount,
+                 0,
+                 FixedMtrrMemoryLimit,
+                 CacheUncacheable
+                 );
+      ASSERT (Status != RETURN_OUT_OF_RESOURCES);
+    }
 
     //
     // 2.3. Apply the new memory attribute settings to Ranges.
@@ -2493,13 +2500,13 @@ MtrrSetMemoryAttributesInMtrrSettings (
     for (Index = 0; Index < RangeCount; Index++) {
       BaseAddress = Ranges[Index].BaseAddress;
       Length      = Ranges[Index].Length;
-      if (BaseAddress < BASE_1MB) {
-        if (Length <= BASE_1MB - BaseAddress) {
+      if (BaseAddress < FixedMtrrMemoryLimit) {
+        if (Length <= FixedMtrrMemoryLimit - BaseAddress) {
           continue;
         }
 
-        Length     -= BASE_1MB - BaseAddress;
-        BaseAddress = BASE_1MB;
+        Length     -= FixedMtrrMemoryLimit - BaseAddress;
+        BaseAddress = FixedMtrrMemoryLimit;
       }
 
       Status = MtrrLibSetMemoryType (
@@ -2544,7 +2551,7 @@ MtrrSetMemoryAttributesInMtrrSettings (
       // 2.5. Remove the [0, 1MB) MTRR if it still exists (not merged with other range)
       //
       for (Index = 0; Index < WorkingVariableMtrrCount; Index++) {
-        if ((WorkingVariableMtrr[Index].BaseAddress == 0) && (WorkingVariableMtrr[Index].Length == SIZE_1MB)) {
+        if ((WorkingVariableMtrr[Index].BaseAddress == 0) && (WorkingVariableMtrr[Index].Length == FixedMtrrMemoryLimit)) {
           ASSERT (WorkingVariableMtrr[Index].Type == CacheUncacheable);
           WorkingVariableMtrrCount--;
           CopyMem (
@@ -2583,7 +2590,7 @@ MtrrSetMemoryAttributesInMtrrSettings (
   ZeroMem (ClearMasks, sizeof (ClearMasks));
   ZeroMem (OrMasks, sizeof (OrMasks));
   for (Index = 0; Index < RangeCount; Index++) {
-    if (Ranges[Index].BaseAddress >= BASE_1MB) {
+    if (Ranges[Index].BaseAddress >= FixedMtrrMemoryLimit) {
       continue;
     }
 
@@ -2606,11 +2613,19 @@ MtrrSetMemoryAttributesInMtrrSettings (
   for (Index = 0; Index < ARRAY_SIZE (ClearMasks); Index++) {
     if (ClearMasks[Index] != 0) {
       if (MtrrSetting != NULL) {
-        MtrrSetting->Fixed.Mtrr[Index] = (MtrrSetting->Fixed.Mtrr[Index] & ~ClearMasks[Index]) | OrMasks[Index];
+        //
+        // Fixed MTRR is modified indicating fixed MTRR should be enabled in the end of MTRR programming.
+        //
+        ((MSR_IA32_MTRR_DEF_TYPE_REGISTER *)&MtrrSetting->MtrrDefType)->Bits.FE = 1;
+        MtrrSetting->Fixed.Mtrr[Index]                                          = (MtrrSetting->Fixed.Mtrr[Index] & ~ClearMasks[Index]) | OrMasks[Index];
       } else {
         if (!MtrrContextValid) {
           MtrrLibPreMtrrChange (&MtrrContext);
-          MtrrContextValid = TRUE;
+          //
+          // Fixed MTRR is modified indicating fixed MTRR should be enabled in the end of MTRR programming.
+          //
+          MtrrContext.DefType.Bits.FE = 1;
+          MtrrContextValid            = TRUE;
         }
 
         AsmMsrAndThenOr64 (mMtrrLibFixedMtrrTable[Index].Msr, ~ClearMasks[Index], OrMasks[Index]);
@@ -2653,8 +2668,10 @@ MtrrSetMemoryAttributesInMtrrSettings (
   }
 
   if (MtrrSetting != NULL) {
-    ((MSR_IA32_MTRR_DEF_TYPE_REGISTER *)&MtrrSetting->MtrrDefType)->Bits.E  = 1;
-    ((MSR_IA32_MTRR_DEF_TYPE_REGISTER *)&MtrrSetting->MtrrDefType)->Bits.FE = 1;
+    //
+    // Enable MTRR unconditionally
+    //
+    ((MSR_IA32_MTRR_DEF_TYPE_REGISTER *)&MtrrSetting->MtrrDefType)->Bits.E = 1;
   } else {
     if (MtrrContextValid) {
       MtrrLibPostMtrrChange (&MtrrContext);
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108568): https://edk2.groups.io/g/devel/message/108568
Mute This Topic: https://groups.io/mt/101331030/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 13/16] UefiCpuPkg/MtrrLib: Add API MtrrGetMemoryAttributesInMtrrSettings.
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (11 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 12/16] UefiCpuPkg/MtrrLib: Update APIs related to set memory attributes Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 14/16] UefiCpuPkg/MtrrLib: Improve MtrrDebugPrintAllMtrrsWorker Yuanhao Xie
                   ` (4 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Eric Dong, Rahul Kumar, Gerd Hoffmann

MtrrGetMemoryAttributesInMtrrSettings parses the MTRR settings
either from hardware or from the parameter and returns an
array containing the memory cache types of all memory addresses.

This API could elinimate the needs of following APIs:
1. MtrrGetMemoryAttributeInVariableMtr
2. MtrrGetFixedMtrr

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Include/Library/MtrrLib.h |  23 +++++++++++++++++++++++
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 130 insertions(+), 10 deletions(-)

diff --git a/UefiCpuPkg/Include/Library/MtrrLib.h b/UefiCpuPkg/Include/Library/MtrrLib.h
index 94cf615901..86cc1aab3b 100644
--- a/UefiCpuPkg/Include/Library/MtrrLib.h
+++ b/UefiCpuPkg/Include/Library/MtrrLib.h
@@ -359,4 +359,27 @@ MtrrSetMemoryAttributesInMtrrSettings (
   IN     UINTN                    RangeCount
   );
 
+/**
+  This function returns a Ranges array containing the memory cache types
+  of all memory addresses.
+
+  @param[in]      MtrrSetting  MTRR setting buffer to parse.
+  @param[out]     Ranges       Pointer to an array of MTRR_MEMORY_RANGE.
+  @param[in,out]  RangeCount   Count of MTRR_MEMORY_RANGE.
+                               On input, the maximum entries the Ranges can hold.
+                               On output, the actual entries that the function returns.
+
+  @retval RETURN_INVALID_PARAMETER RangeCount is NULL.
+  @retval RETURN_INVALID_PARAMETER *RangeCount is not 0 but Ranges is NULL.
+  @retval RETURN_BUFFER_TOO_SMALL  *RangeCount is too small.
+  @retval RETURN_SUCCESS           Ranges are successfully returned.
+**/
+RETURN_STATUS
+EFIAPI
+MtrrGetMemoryAttributesInMtrrSettings (
+  IN CONST MTRR_SETTINGS      *MtrrSetting OPTIONAL,
+  OUT      MTRR_MEMORY_RANGE  *Ranges,
+  IN OUT   UINTN              *RangeCount
+  );
+
 #endif // _MTRR_LIB_H_
diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
index 6ba0c9b37f..61c81c5139 100644
--- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
+++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
@@ -292,7 +292,7 @@ GetFirmwareVariableMtrrCount (
 **/
 MTRR_MEMORY_CACHE_TYPE
 MtrrGetDefaultMemoryTypeWorker (
-  IN MTRR_SETTINGS  *MtrrSetting
+  IN CONST MTRR_SETTINGS  *MtrrSetting
   )
 {
   MSR_IA32_MTRR_DEF_TYPE_REGISTER  DefType;
@@ -689,11 +689,11 @@ MtrrGetMemoryAttributeInVariableMtrrWorker (
 **/
 UINT32
 MtrrLibGetRawVariableRanges (
-  IN  MTRR_VARIABLE_SETTINGS  *VariableSettings,
-  IN  UINTN                   VariableMtrrCount,
-  IN  UINT64                  MtrrValidBitsMask,
-  IN  UINT64                  MtrrValidAddressMask,
-  OUT MTRR_MEMORY_RANGE       *VariableMtrr
+  IN  CONST MTRR_VARIABLE_SETTINGS  *VariableSettings,
+  IN  UINTN                         VariableMtrrCount,
+  IN  UINT64                        MtrrValidBitsMask,
+  IN  UINT64                        MtrrValidAddressMask,
+  OUT MTRR_MEMORY_RANGE             *VariableMtrr
   )
 {
   UINTN   Index;
@@ -1823,10 +1823,10 @@ MtrrLibCalculateMtrrs (
 **/
 RETURN_STATUS
 MtrrLibApplyFixedMtrrs (
-  IN     MTRR_FIXED_SETTINGS  *Fixed,
-  IN OUT MTRR_MEMORY_RANGE    *Ranges,
-  IN     UINTN                RangeCapacity,
-  IN OUT UINTN                *RangeCount
+  IN     CONST MTRR_FIXED_SETTINGS  *Fixed,
+  IN OUT MTRR_MEMORY_RANGE          *Ranges,
+  IN     UINTN                      RangeCapacity,
+  IN OUT UINTN                      *RangeCount
   )
 {
   RETURN_STATUS           Status;
@@ -2956,6 +2956,103 @@ IsMtrrSupported (
   return MtrrLibIsMtrrSupported (NULL, NULL);
 }
 
+/**
+  This function returns a Ranges array containing the memory cache types
+  of all memory addresses.
+
+  @param[in]      MtrrSetting  MTRR setting buffer to parse.
+  @param[out]     Ranges       Pointer to an array of MTRR_MEMORY_RANGE.
+  @param[in,out]  RangeCount   Count of MTRR_MEMORY_RANGE.
+                               On input, the maximum entries the Ranges can hold.
+                               On output, the actual entries that the function returns.
+
+  @retval RETURN_INVALID_PARAMETER RangeCount is NULL.
+  @retval RETURN_INVALID_PARAMETER *RangeCount is not 0 but Ranges is NULL.
+  @retval RETURN_BUFFER_TOO_SMALL  *RangeCount is too small.
+  @retval RETURN_SUCCESS           Ranges are successfully returned.
+**/
+RETURN_STATUS
+EFIAPI
+MtrrGetMemoryAttributesInMtrrSettings (
+  IN CONST MTRR_SETTINGS      *MtrrSetting OPTIONAL,
+  OUT      MTRR_MEMORY_RANGE  *Ranges,
+  IN OUT   UINTN              *RangeCount
+  )
+{
+  RETURN_STATUS                    Status;
+  MTRR_SETTINGS                    LocalMtrrs;
+  CONST MTRR_SETTINGS              *Mtrrs;
+  MSR_IA32_MTRR_DEF_TYPE_REGISTER  *MtrrDefType;
+  UINTN                            LocalRangeCount;
+  UINT64                           MtrrValidBitsMask;
+  UINT64                           MtrrValidAddressMask;
+  UINT32                           VariableMtrrCount;
+  MTRR_MEMORY_RANGE                RawVariableRanges[ARRAY_SIZE (Mtrrs->Variables.Mtrr)];
+  MTRR_MEMORY_RANGE                LocalRanges[
+                                               ARRAY_SIZE (mMtrrLibFixedMtrrTable) * sizeof (UINT64) + 2 * ARRAY_SIZE (Mtrrs->Variables.Mtrr) + 1
+  ];
+
+  if (RangeCount == NULL) {
+    return RETURN_INVALID_PARAMETER;
+  }
+
+  if ((*RangeCount != 0) && (Ranges == NULL)) {
+    return RETURN_INVALID_PARAMETER;
+  }
+
+  if (MtrrSetting != NULL) {
+    Mtrrs = MtrrSetting;
+  } else {
+    MtrrGetAllMtrrs (&LocalMtrrs);
+    Mtrrs = &LocalMtrrs;
+  }
+
+  MtrrDefType = (MSR_IA32_MTRR_DEF_TYPE_REGISTER *)&Mtrrs->MtrrDefType;
+
+  LocalRangeCount = 1;
+  MtrrLibInitializeMtrrMask (&MtrrValidBitsMask, &MtrrValidAddressMask);
+  LocalRanges[0].BaseAddress = 0;
+  LocalRanges[0].Length      = MtrrValidBitsMask + 1;
+
+  if (MtrrDefType->Bits.E == 0) {
+    LocalRanges[0].Type = CacheUncacheable;
+  } else {
+    LocalRanges[0].Type = MtrrGetDefaultMemoryTypeWorker (Mtrrs);
+
+    VariableMtrrCount = GetVariableMtrrCountWorker ();
+    ASSERT (VariableMtrrCount <= ARRAY_SIZE (MtrrSetting->Variables.Mtrr));
+
+    MtrrLibGetRawVariableRanges (
+      &Mtrrs->Variables,
+      VariableMtrrCount,
+      MtrrValidBitsMask,
+      MtrrValidAddressMask,
+      RawVariableRanges
+      );
+    Status = MtrrLibApplyVariableMtrrs (
+               RawVariableRanges,
+               VariableMtrrCount,
+               LocalRanges,
+               ARRAY_SIZE (LocalRanges),
+               &LocalRangeCount
+               );
+    ASSERT_RETURN_ERROR (Status);
+
+    if (MtrrDefType->Bits.FE == 1) {
+      MtrrLibApplyFixedMtrrs (&Mtrrs->Fixed, LocalRanges, ARRAY_SIZE (LocalRanges), &LocalRangeCount);
+    }
+  }
+
+  if (*RangeCount < LocalRangeCount) {
+    *RangeCount = LocalRangeCount;
+    return RETURN_BUFFER_TOO_SMALL;
+  }
+
+  CopyMem (Ranges, LocalRanges, LocalRangeCount * sizeof (LocalRanges[0]));
+  *RangeCount = LocalRangeCount;
+  return RETURN_SUCCESS;
+}
+
 /**
   Worker function prints all MTRRs for debugging.
 
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108569): https://edk2.groups.io/g/devel/message/108569
Mute This Topic: https://groups.io/mt/101331032/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 14/16] UefiCpuPkg/MtrrLib: Improve MtrrDebugPrintAllMtrrsWorker.
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (12 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 13/16] UefiCpuPkg/MtrrLib: Add API MtrrGetMemoryAttributesInMtrrSettings Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 15/16] UefiCpuPkg/MtrrUnitTest: Add Unit test of setting/getting memory attributes Yuanhao Xie
                   ` (3 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Eric Dong, Rahul Kumar, Gerd Hoffmann

Improve MtrrDebugPrintAllMtrrsWorker by making use of
MtrrGetMemoryAttributesInMtrrSettings.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 45 ++++++++++-----------------------------------
 1 file changed, 10 insertions(+), 35 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
index 61c81c5139..26422204c4 100644
--- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
+++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
@@ -3068,24 +3068,15 @@ MtrrDebugPrintAllMtrrsWorker (
   )
 {
   DEBUG_CODE_BEGIN ();
+  UINT32             Index;
   MTRR_SETTINGS      LocalMtrrs;
   MTRR_SETTINGS      *Mtrrs;
-  UINTN              Index;
+  RETURN_STATUS      Status;
   UINTN              RangeCount;
-  UINT64             MtrrValidBitsMask;
-  UINT64             MtrrValidAddressMask;
-  UINT32             VariableMtrrCount;
   BOOLEAN            ContainVariableMtrr;
   MTRR_MEMORY_RANGE  Ranges[
                             ARRAY_SIZE (mMtrrLibFixedMtrrTable) * sizeof (UINT64) + 2 * ARRAY_SIZE (Mtrrs->Variables.Mtrr) + 1
   ];
-  MTRR_MEMORY_RANGE  RawVariableRanges[ARRAY_SIZE (Mtrrs->Variables.Mtrr)];
-
-  if (!IsMtrrSupported ()) {
-    return;
-  }
-
-  VariableMtrrCount = GetVariableMtrrCountWorker ();
 
   if (MtrrSetting != NULL) {
     Mtrrs = MtrrSetting;
@@ -3094,6 +3085,13 @@ MtrrDebugPrintAllMtrrsWorker (
     Mtrrs = &LocalMtrrs;
   }
 
+  RangeCount = ARRAY_SIZE (Ranges);
+  Status     = MtrrGetMemoryAttributesInMtrrSettings (Mtrrs, Ranges, &RangeCount);
+  if (RETURN_ERROR (Status)) {
+    DEBUG ((DEBUG_CACHE, "MTRR is not enabled.\n"));
+    return;
+  }
+
   //
   // Dump RAW MTRR contents
   //
@@ -3105,7 +3103,7 @@ MtrrDebugPrintAllMtrrsWorker (
   }
 
   ContainVariableMtrr = FALSE;
-  for (Index = 0; Index < VariableMtrrCount; Index++) {
+  for (Index = 0; Index < ARRAY_SIZE (Mtrrs->Variables.Mtrr); Index++) {
     if ((Mtrrs->Variables.Mtrr[Index].Mask & BIT11) == 0) {
       //
       // If mask is not valid, then do not display range
@@ -3134,29 +3132,6 @@ MtrrDebugPrintAllMtrrsWorker (
   //
   DEBUG ((DEBUG_CACHE, "Memory Ranges:\n"));
   DEBUG ((DEBUG_CACHE, "====================================\n"));
-  MtrrLibInitializeMtrrMask (&MtrrValidBitsMask, &MtrrValidAddressMask);
-  Ranges[0].BaseAddress = 0;
-  Ranges[0].Length      = MtrrValidBitsMask + 1;
-  Ranges[0].Type        = MtrrGetDefaultMemoryTypeWorker (Mtrrs);
-  RangeCount            = 1;
-
-  MtrrLibGetRawVariableRanges (
-    &Mtrrs->Variables,
-    VariableMtrrCount,
-    MtrrValidBitsMask,
-    MtrrValidAddressMask,
-    RawVariableRanges
-    );
-  MtrrLibApplyVariableMtrrs (
-    RawVariableRanges,
-    VariableMtrrCount,
-    Ranges,
-    ARRAY_SIZE (Ranges),
-    &RangeCount
-    );
-
-  MtrrLibApplyFixedMtrrs (&Mtrrs->Fixed, Ranges, ARRAY_SIZE (Ranges), &RangeCount);
-
   for (Index = 0; Index < RangeCount; Index++) {
     DEBUG ((
       DEBUG_CACHE,
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108570): https://edk2.groups.io/g/devel/message/108570
Mute This Topic: https://groups.io/mt/101331033/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 15/16] UefiCpuPkg/MtrrUnitTest: Add Unit test of setting/getting memory attributes
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (13 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 14/16] UefiCpuPkg/MtrrLib: Improve MtrrDebugPrintAllMtrrsWorker Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-09-13  4:26 ` [edk2-devel] [PATCH 16/16] UefiCpuPkg/CpuDxe: Update RefreshMemoryAttributesFromMtrr Yuanhao Xie
                   ` (2 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Yuanhao Xie, Eric Dong, Rahul Kumar, Gerd Hoffmann

Add Unit test of trrGetMemoryAttributesInMtrrSettings and
MtrrSetMemoryAttributesInMtrrSettings.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c | 36 ++++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
index dc348ef0a3..b252eacf34 100644
--- a/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
+++ b/UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c
@@ -177,7 +177,8 @@ GenerateRandomMemoryTypeCombination (
 }
 
 /**
-  Unit test of MtrrLib service MtrrSetMemoryAttribute()
+  Unit test of MtrrLib service MtrrGetMemoryAttributesInMtrrSettings() and
+  MtrrSetMemoryAttributesInMtrrSettings()
 
   @param[in]  Context    Ignored
 
@@ -188,7 +189,7 @@ GenerateRandomMemoryTypeCombination (
 **/
 UNIT_TEST_STATUS
 EFIAPI
-UnitTestMtrrSetMemoryAttributesInMtrrSettings (
+UnitTestMtrrSetAndGetMemoryAttributesInMtrrSettings (
   IN UNIT_TEST_CONTEXT  Context
   )
 {
@@ -214,6 +215,9 @@ UnitTestMtrrSetMemoryAttributesInMtrrSettings (
   UINT32             ActualVariableMtrrUsage;
   UINTN              ActualMemoryRangesCount;
 
+  MTRR_MEMORY_RANGE  ReturnedMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR   * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
+  UINTN              ReturnedMemoryRangesCount;
+
   MTRR_SETTINGS  *Mtrrs[2];
 
   SystemParameter = (MTRR_LIB_SYSTEM_PARAMETER *)Context;
@@ -292,12 +296,22 @@ UnitTestMtrrSetMemoryAttributesInMtrrSettings (
       &ActualMemoryRangesCount,
       &ActualVariableMtrrUsage
       );
-
     UT_LOG_INFO ("--- Actual Memory Ranges [%d] ---\n", ActualMemoryRangesCount);
     DumpMemoryRanges (ActualMemoryRanges, ActualMemoryRangesCount);
     VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, ActualMemoryRanges, ActualMemoryRangesCount);
     UT_ASSERT_TRUE (ExpectedVariableMtrrUsage >= ActualVariableMtrrUsage);
 
+    ReturnedMemoryRangesCount = ARRAY_SIZE (ReturnedMemoryRanges);
+    Status                    = MtrrGetMemoryAttributesInMtrrSettings (
+                                  Mtrrs[MtrrIndex],
+                                  ReturnedMemoryRanges,
+                                  &ReturnedMemoryRangesCount
+                                  );
+    UT_ASSERT_STATUS_EQUAL (Status, RETURN_SUCCESS);
+    UT_LOG_INFO ("--- Returned Memory Ranges [%d] ---\n", ReturnedMemoryRangesCount);
+    DumpMemoryRanges (ReturnedMemoryRanges, ReturnedMemoryRangesCount);
+    VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, ReturnedMemoryRanges, ReturnedMemoryRangesCount);
+
     ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
   }
 
@@ -1041,6 +1055,9 @@ UnitTestMtrrSetMemoryAttributeInMtrrSettings (
   UINT32             ActualVariableMtrrUsage;
   UINTN              ActualMemoryRangesCount;
 
+  MTRR_MEMORY_RANGE  ReturnedMemoryRanges[MTRR_NUMBER_OF_FIXED_MTRR * sizeof (UINT64) + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1];
+  UINTN              ReturnedMemoryRangesCount;
+
   MTRR_SETTINGS  *Mtrrs[2];
 
   SystemParameter = (MTRR_LIB_SYSTEM_PARAMETER *)Context;
@@ -1117,6 +1134,17 @@ UnitTestMtrrSetMemoryAttributeInMtrrSettings (
     VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, ActualMemoryRanges, ActualMemoryRangesCount);
     UT_ASSERT_TRUE (ExpectedVariableMtrrUsage >= ActualVariableMtrrUsage);
 
+    ReturnedMemoryRangesCount = ARRAY_SIZE (ReturnedMemoryRanges);
+    Status                    = MtrrGetMemoryAttributesInMtrrSettings (
+                                  &LocalMtrrs,
+                                  ReturnedMemoryRanges,
+                                  &ReturnedMemoryRangesCount
+                                  );
+    UT_ASSERT_STATUS_EQUAL (Status, RETURN_SUCCESS);
+    UT_LOG_INFO ("--- Returned Memory Ranges [%d] ---\n", ReturnedMemoryRangesCount);
+    DumpMemoryRanges (ReturnedMemoryRanges, ReturnedMemoryRangesCount);
+    VerifyMemoryRanges (ExpectedMemoryRanges, ExpectedMemoryRangesCount, ReturnedMemoryRanges, ReturnedMemoryRangesCount);
+
     ZeroMem (&LocalMtrrs, sizeof (LocalMtrrs));
   }
 
@@ -1226,7 +1254,7 @@ UnitTestingEntry (
     for (Index = 0; Index < Iteration; Index++) {
       AddTestCase (MtrrApiTests, "Test InvalidMemoryLayouts", "InvalidMemoryLayouts", UnitTestInvalidMemoryLayouts, InitializeSystem, NULL, &mSystemParameters[SystemIndex]);
       AddTestCase (MtrrApiTests, "Test MtrrSetMemoryAttributeInMtrrSettings", "MtrrSetMemoryAttributeInMtrrSettings", UnitTestMtrrSetMemoryAttributeInMtrrSettings, InitializeSystem, NULL, &mSystemParameters[SystemIndex]);
-      AddTestCase (MtrrApiTests, "Test MtrrSetMemoryAttributesInMtrrSettings", "MtrrSetMemoryAttributesInMtrrSettings", UnitTestMtrrSetMemoryAttributesInMtrrSettings, InitializeSystem, NULL, &mSystemParameters[SystemIndex]);
+      AddTestCase (MtrrApiTests, "Test MtrrSetAndGetMemoryAttributesInMtrrSettings", "MtrrSetAndGetMemoryAttributesInMtrrSettings", UnitTestMtrrSetAndGetMemoryAttributesInMtrrSettings, InitializeSystem, NULL, &mSystemParameters[SystemIndex]);
     }
   }
 
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108571): https://edk2.groups.io/g/devel/message/108571
Mute This Topic: https://groups.io/mt/101331034/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [edk2-devel] [PATCH 16/16] UefiCpuPkg/CpuDxe: Update RefreshMemoryAttributesFromMtrr.
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (14 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 15/16] UefiCpuPkg/MtrrUnitTest: Add Unit test of setting/getting memory attributes Yuanhao Xie
@ 2023-09-13  4:26 ` Yuanhao Xie
  2023-10-01  9:30 ` [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Ni, Ray
  2023-10-09  6:27 ` Dong, Eric
  17 siblings, 0 replies; 19+ messages in thread
From: Yuanhao Xie @ 2023-09-13  4:26 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Eric Dong, Rahul Kumar, Gerd Hoffmann

From: Ray Ni <ray.ni@intel.com>

Old implementation of RefreshMemoryAttributesFromMtrr directly
retrieves the MTRR register content and applies the MTRR cache type
to GCD database following the precedence order defined by SDM.

The code can updated to simply get all the memory cache types for all
memory through newly introduced API With the new introduced API
MtrrGetMemoryAttributesInMtrrSettings.

Benefits:
1. Remove the duplicated logic in CpuDxe driver that handles MTRR
    details.
2. Let the MtrrLib to handle the case when fixed MTRR is absent.
    (Old logic cannot handle the case.)

Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/CpuDxe/CpuDxe.c | 309 ++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1 file changed, 36 insertions(+), 273 deletions(-)

diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.c b/UefiCpuPkg/CpuDxe/CpuDxe.c
index 804ef5d1fe..c5ac624f9e 100644
--- a/UefiCpuPkg/CpuDxe/CpuDxe.c
+++ b/UefiCpuPkg/CpuDxe/CpuDxe.c
@@ -15,73 +15,15 @@
 //
 // Global Variables
 //
-BOOLEAN     InterruptState = FALSE;
-EFI_HANDLE  mCpuHandle     = NULL;
-BOOLEAN     mIsFlushingGCD;
-BOOLEAN     mIsAllocatingPageTable = FALSE;
-UINT64      mValidMtrrAddressMask;
-UINT64      mValidMtrrBitsMask;
-UINT64      mTimerPeriod = 0;
-
-FIXED_MTRR  mFixedMtrrTable[] = {
-  {
-    MSR_IA32_MTRR_FIX64K_00000,
-    0,
-    0x10000
-  },
-  {
-    MSR_IA32_MTRR_FIX16K_80000,
-    0x80000,
-    0x4000
-  },
-  {
-    MSR_IA32_MTRR_FIX16K_A0000,
-    0xA0000,
-    0x4000
-  },
-  {
-    MSR_IA32_MTRR_FIX4K_C0000,
-    0xC0000,
-    0x1000
-  },
-  {
-    MSR_IA32_MTRR_FIX4K_C8000,
-    0xC8000,
-    0x1000
-  },
-  {
-    MSR_IA32_MTRR_FIX4K_D0000,
-    0xD0000,
-    0x1000
-  },
-  {
-    MSR_IA32_MTRR_FIX4K_D8000,
-    0xD8000,
-    0x1000
-  },
-  {
-    MSR_IA32_MTRR_FIX4K_E0000,
-    0xE0000,
-    0x1000
-  },
-  {
-    MSR_IA32_MTRR_FIX4K_E8000,
-    0xE8000,
-    0x1000
-  },
-  {
-    MSR_IA32_MTRR_FIX4K_F0000,
-    0xF0000,
-    0x1000
-  },
-  {
-    MSR_IA32_MTRR_FIX4K_F8000,
-    0xF8000,
-    0x1000
-  },
-};
-
-EFI_CPU_ARCH_PROTOCOL  gCpu = {
+BOOLEAN                InterruptState = FALSE;
+EFI_HANDLE             mCpuHandle     = NULL;
+BOOLEAN                mIsFlushingGCD;
+BOOLEAN                mIsAllocatingPageTable = FALSE;
+UINT64                 mValidMtrrAddressMask;
+UINT64                 mValidMtrrBitsMask;
+UINT64                 mTimerPeriod     = 0;
+UINT32                 mCpuTargetCState = 0;
+EFI_CPU_ARCH_PROTOCOL  gCpu             = {
   CpuFlushCpuDataCache,
   CpuEnableInterrupt,
   CpuDisableInterrupt,
@@ -494,77 +436,31 @@ CpuSetMemoryAttributes (
   return AssignMemoryPageAttributes (NULL, BaseAddress, Length, MemoryAttributes, NULL);
 }
 
-/**
-  Initializes the valid bits mask and valid address mask for MTRRs.
-
-  This function initializes the valid bits mask and valid address mask for MTRRs.
-
-**/
-VOID
-InitializeMtrrMask (
-  VOID
-  )
-{
-  UINT32                                       MaxExtendedFunction;
-  CPUID_VIR_PHY_ADDRESS_SIZE_EAX               VirPhyAddressSize;
-  UINT32                                       MaxFunction;
-  CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_ECX  ExtendedFeatureFlagsEcx;
-  MSR_IA32_TME_ACTIVATE_REGISTER               TmeActivate;
-
-  AsmCpuid (CPUID_EXTENDED_FUNCTION, &MaxExtendedFunction, NULL, NULL, NULL);
-
-  if (MaxExtendedFunction >= CPUID_VIR_PHY_ADDRESS_SIZE) {
-    AsmCpuid (CPUID_VIR_PHY_ADDRESS_SIZE, &VirPhyAddressSize.Uint32, NULL, NULL, NULL);
-  } else {
-    VirPhyAddressSize.Bits.PhysicalAddressBits = 36;
-  }
-
-  //
-  // CPUID enumeration of MAX_PA is unaffected by TME-MK activation and will continue
-  // to report the maximum physical address bits available for software to use,
-  // irrespective of the number of KeyID bits.
-  // So, we need to check if TME is enabled and adjust the PA size accordingly.
-  //
-  AsmCpuid (CPUID_SIGNATURE, &MaxFunction, NULL, NULL, NULL);
-  if (MaxFunction >= CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS) {
-    AsmCpuidEx (CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS, 0, NULL, NULL, &ExtendedFeatureFlagsEcx.Uint32, NULL);
-    if (ExtendedFeatureFlagsEcx.Bits.TME_EN == 1) {
-      TmeActivate.Uint64 = AsmReadMsr64 (MSR_IA32_TME_ACTIVATE);
-      if (TmeActivate.Bits.TmeEnable == 1) {
-        VirPhyAddressSize.Bits.PhysicalAddressBits -= TmeActivate.Bits.MkTmeKeyidBits;
-      }
-    }
-  }
-
-  mValidMtrrBitsMask    = LShiftU64 (1, VirPhyAddressSize.Bits.PhysicalAddressBits) - 1;
-  mValidMtrrAddressMask = mValidMtrrBitsMask & 0xfffffffffffff000ULL;
-}
-
 /**
   Gets GCD Mem Space type from MTRR Type.
 
   This function gets GCD Mem Space type from MTRR Type.
 
-  @param  MtrrAttributes  MTRR memory type
+  @param  Type  MTRR memory type
 
   @return GCD Mem Space type
 
 **/
 UINT64
 GetMemorySpaceAttributeFromMtrrType (
-  IN UINT8  MtrrAttributes
+  IN MTRR_MEMORY_CACHE_TYPE  Type
   )
 {
-  switch (MtrrAttributes) {
-    case MTRR_CACHE_UNCACHEABLE:
+  switch (Type) {
+    case CacheUncacheable:
       return EFI_MEMORY_UC;
-    case MTRR_CACHE_WRITE_COMBINING:
+    case CacheWriteCombining:
       return EFI_MEMORY_WC;
-    case MTRR_CACHE_WRITE_THROUGH:
+    case CacheWriteThrough:
       return EFI_MEMORY_WT;
-    case MTRR_CACHE_WRITE_PROTECTED:
+    case CacheWriteProtected:
       return EFI_MEMORY_WP;
-    case MTRR_CACHE_WRITE_BACK:
+    case CacheWriteBack:
       return EFI_MEMORY_WB;
     default:
       return 0;
@@ -716,41 +612,15 @@ RefreshMemoryAttributesFromMtrr (
   )
 {
   EFI_STATUS                       Status;
+  RETURN_STATUS                    ReturnStatus;
   UINTN                            Index;
-  UINTN                            SubIndex;
-  UINT64                           RegValue;
-  EFI_PHYSICAL_ADDRESS             BaseAddress;
-  UINT64                           Length;
-  UINT64                           Attributes;
-  UINT64                           CurrentAttributes;
-  UINT8                            MtrrType;
   UINTN                            NumberOfDescriptors;
   EFI_GCD_MEMORY_SPACE_DESCRIPTOR  *MemorySpaceMap;
-  UINT64                           DefaultAttributes;
-  VARIABLE_MTRR                    VariableMtrr[MTRR_NUMBER_OF_VARIABLE_MTRR];
-  MTRR_FIXED_SETTINGS              MtrrFixedSettings;
-  UINT32                           FirmwareVariableMtrrCount;
-  UINT8                            DefaultMemoryType;
-
-  FirmwareVariableMtrrCount = GetFirmwareVariableMtrrCount ();
-  ASSERT (FirmwareVariableMtrrCount <= MTRR_NUMBER_OF_VARIABLE_MTRR);
+  MTRR_MEMORY_RANGE                *Ranges;
+  UINTN                            RangeCount;
 
   MemorySpaceMap = NULL;
 
-  //
-  // Initialize the valid bits mask and valid address mask for MTRRs
-  //
-  InitializeMtrrMask ();
-
-  //
-  // Get the memory attribute of variable MTRRs
-  //
-  MtrrGetMemoryAttributeInVariableMtrr (
-    mValidMtrrBitsMask,
-    mValidMtrrAddressMask,
-    VariableMtrr
-    );
-
   //
   // Get the memory space map from GCD
   //
@@ -760,131 +630,24 @@ RefreshMemoryAttributesFromMtrr (
                   );
   ASSERT_EFI_ERROR (Status);
 
-  DefaultMemoryType = (UINT8)MtrrGetDefaultMemoryType ();
-  DefaultAttributes = GetMemorySpaceAttributeFromMtrrType (DefaultMemoryType);
-
-  //
-  // Set default attributes to all spaces.
-  //
-  for (Index = 0; Index < NumberOfDescriptors; Index++) {
-    if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeNonExistent) {
-      continue;
-    }
-
-    gDS->SetMemorySpaceAttributes (
-           MemorySpaceMap[Index].BaseAddress,
-           MemorySpaceMap[Index].Length,
-           (MemorySpaceMap[Index].Attributes & ~EFI_CACHE_ATTRIBUTE_MASK) |
-           (MemorySpaceMap[Index].Capabilities & DefaultAttributes)
-           );
-  }
-
-  //
-  // Go for variable MTRRs with WB attribute
-  //
-  for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {
-    if (VariableMtrr[Index].Valid &&
-        (VariableMtrr[Index].Type == MTRR_CACHE_WRITE_BACK))
-    {
-      SetGcdMemorySpaceAttributes (
-        MemorySpaceMap,
-        NumberOfDescriptors,
-        VariableMtrr[Index].BaseAddress,
-        VariableMtrr[Index].Length,
-        EFI_MEMORY_WB
-        );
-    }
-  }
-
-  //
-  // Go for variable MTRRs with the attribute except for WB and UC attributes
-  //
-  for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {
-    if (VariableMtrr[Index].Valid &&
-        (VariableMtrr[Index].Type != MTRR_CACHE_WRITE_BACK) &&
-        (VariableMtrr[Index].Type != MTRR_CACHE_UNCACHEABLE))
-    {
-      Attributes = GetMemorySpaceAttributeFromMtrrType ((UINT8)VariableMtrr[Index].Type);
-      SetGcdMemorySpaceAttributes (
-        MemorySpaceMap,
-        NumberOfDescriptors,
-        VariableMtrr[Index].BaseAddress,
-        VariableMtrr[Index].Length,
-        Attributes
-        );
-    }
+  RangeCount   = 0;
+  ReturnStatus = MtrrGetMemoryAttributesInMtrrSettings (NULL, NULL, &RangeCount);
+  ASSERT (ReturnStatus == RETURN_BUFFER_TOO_SMALL);
+  Ranges = AllocatePool (sizeof (*Ranges) * RangeCount);
+  ASSERT (Ranges != NULL);
+  ReturnStatus = MtrrGetMemoryAttributesInMtrrSettings (NULL, Ranges, &RangeCount);
+  ASSERT_RETURN_ERROR (ReturnStatus);
+
+  for (Index = 0; Index < RangeCount; Index++) {
+    SetGcdMemorySpaceAttributes (
+      MemorySpaceMap,
+      NumberOfDescriptors,
+      Ranges[Index].BaseAddress,
+      Ranges[Index].Length,
+      GetMemorySpaceAttributeFromMtrrType (Ranges[Index].Type)
+      );
   }
 
-  //
-  // Go for variable MTRRs with UC attribute
-  //
-  for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {
-    if (VariableMtrr[Index].Valid &&
-        (VariableMtrr[Index].Type == MTRR_CACHE_UNCACHEABLE))
-    {
-      SetGcdMemorySpaceAttributes (
-        MemorySpaceMap,
-        NumberOfDescriptors,
-        VariableMtrr[Index].BaseAddress,
-        VariableMtrr[Index].Length,
-        EFI_MEMORY_UC
-        );
-    }
-  }
-
-  //
-  // Go for fixed MTRRs
-  //
-  Attributes  = 0;
-  BaseAddress = 0;
-  Length      = 0;
-  MtrrGetFixedMtrr (&MtrrFixedSettings);
-  for (Index = 0; Index < MTRR_NUMBER_OF_FIXED_MTRR; Index++) {
-    RegValue = MtrrFixedSettings.Mtrr[Index];
-    //
-    // Check for continuous fixed MTRR sections
-    //
-    for (SubIndex = 0; SubIndex < 8; SubIndex++) {
-      MtrrType          = (UINT8)RShiftU64 (RegValue, SubIndex * 8);
-      CurrentAttributes = GetMemorySpaceAttributeFromMtrrType (MtrrType);
-      if (Length == 0) {
-        //
-        // A new MTRR attribute begins
-        //
-        Attributes = CurrentAttributes;
-      } else {
-        //
-        // If fixed MTRR attribute changed, then set memory attribute for previous attribute
-        //
-        if (CurrentAttributes != Attributes) {
-          SetGcdMemorySpaceAttributes (
-            MemorySpaceMap,
-            NumberOfDescriptors,
-            BaseAddress,
-            Length,
-            Attributes
-            );
-          BaseAddress = mFixedMtrrTable[Index].BaseAddress + mFixedMtrrTable[Index].Length * SubIndex;
-          Length      = 0;
-          Attributes  = CurrentAttributes;
-        }
-      }
-
-      Length += mFixedMtrrTable[Index].Length;
-    }
-  }
-
-  //
-  // Handle the last fixed MTRR region
-  //
-  SetGcdMemorySpaceAttributes (
-    MemorySpaceMap,
-    NumberOfDescriptors,
-    BaseAddress,
-    Length,
-    Attributes
-    );
-
   //
   // Free memory space map allocated by GCD service GetMemorySpaceMap ()
   //
-- 
2.36.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108572): https://edk2.groups.io/g/devel/message/108572
Mute This Topic: https://groups.io/mt/101331035/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (15 preceding siblings ...)
  2023-09-13  4:26 ` [edk2-devel] [PATCH 16/16] UefiCpuPkg/CpuDxe: Update RefreshMemoryAttributesFromMtrr Yuanhao Xie
@ 2023-10-01  9:30 ` Ni, Ray
  2023-10-09  6:27 ` Dong, Eric
  17 siblings, 0 replies; 19+ messages in thread
From: Ni, Ray @ 2023-10-01  9:30 UTC (permalink / raw)
  To: devel@edk2.groups.io, Xie, Yuanhao

[-- Attachment #1: Type: text/plain, Size: 3418 bytes --]

Reviewed-by: Ray Ni <ray.ni@intel.com>

Thanks,
Ray
________________________________
From: devel@edk2.groups.io <devel@edk2.groups.io> on behalf of Yuanhao Xie <yuanhao.xie@intel.com>
Sent: Wednesday, September 13, 2023 12:26 PM
To: devel@edk2.groups.io <devel@edk2.groups.io>
Subject: [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement

Update MtrrLib modules to handle the absense of Fixed MTRRs
Update unit test of Update for the absense of Fixed MTRRs

Ray Ni (6):
  UefiCpuPkg/MtrrLib: Add internal function MtrrLibIsMtrrSupported.
  UefiCpuPkg/MtrrUnitTest: Update test to cover no-fixed-mtrr cases.
  UefiCpuPkg/MtrrLib: Fix MtrrGetAllMtrrs to return correct MTRR
    setting.
  UefiCpuPkg/MtrrLib: Fix MtrrSetAllMtrrs to handle absent fixed MTRRs.
  UefiCpuPkg/MtrrLib: Update APIs related to set memory attributes.
  UefiCpuPkg/CpuDxe: Update RefreshMemoryAttributesFromMtrr.

YuanhaoXie (10):
  UefiCpuPkg/MtrrUnitTest: Update the Unit Test for IsMtrrSupported().
  UefiCpuPkg/MtrrUnitTest: Update UnitTestGetFirmwareVariableMtrrCount.
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetDefaultMemoryType.
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetAllMtrrs().
  UefiCpuPkg/MtrrLib: Update MtrrGetFixedMtrr().
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetFixedMtrr().
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrSetAllMtrrs().
  UefiCpuPkg/MtrrLib: Add API MtrrGetMemoryAttributesInMtrrSettings.
  UefiCpuPkg/MtrrLib: Improve MtrrDebugPrintAllMtrrsWorker.
  UefiCpuPkg/MtrrUnitTest: Add Unit test of setting/getting memory
    attributes

 UefiCpuPkg/CpuDxe/CpuDxe.c                            | 309 ++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 UefiCpuPkg/Include/Library/MtrrLib.h                  |  32 +++++++++++++++++++++++++++++---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c                  | 391 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------
 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------
 UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c         |  24 +++++++++++++++---------
 5 files changed, 532 insertions(+), 468 deletions(-)

--
2.36.1.windows.1








-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#109238): https://edk2.groups.io/g/devel/message/109238
Mute This Topic: https://groups.io/mt/101331015/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/leave/12367111/7686176/1913456212/xyzzy [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



[-- Attachment #2: Type: text/html, Size: 5813 bytes --]

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement
  2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
                   ` (16 preceding siblings ...)
  2023-10-01  9:30 ` [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Ni, Ray
@ 2023-10-09  6:27 ` Dong, Eric
  17 siblings, 0 replies; 19+ messages in thread
From: Dong, Eric @ 2023-10-09  6:27 UTC (permalink / raw)
  To: devel@edk2.groups.io, Xie, Yuanhao

Reviewed-by: Eric Dong <eric.dong@intel.com>

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Yuanhao Xie
Sent: Wednesday, September 13, 2023 12:26 PM
To: devel@edk2.groups.io
Subject: [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement

Update MtrrLib modules to handle the absense of Fixed MTRRs Update unit test of Update for the absense of Fixed MTRRs

Ray Ni (6):
  UefiCpuPkg/MtrrLib: Add internal function MtrrLibIsMtrrSupported.
  UefiCpuPkg/MtrrUnitTest: Update test to cover no-fixed-mtrr cases.
  UefiCpuPkg/MtrrLib: Fix MtrrGetAllMtrrs to return correct MTRR
    setting.
  UefiCpuPkg/MtrrLib: Fix MtrrSetAllMtrrs to handle absent fixed MTRRs.
  UefiCpuPkg/MtrrLib: Update APIs related to set memory attributes.
  UefiCpuPkg/CpuDxe: Update RefreshMemoryAttributesFromMtrr.

YuanhaoXie (10):
  UefiCpuPkg/MtrrUnitTest: Update the Unit Test for IsMtrrSupported().
  UefiCpuPkg/MtrrUnitTest: Update UnitTestGetFirmwareVariableMtrrCount.
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetDefaultMemoryType.
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetAllMtrrs().
  UefiCpuPkg/MtrrLib: Update MtrrGetFixedMtrr().
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetFixedMtrr().
  UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrSetAllMtrrs().
  UefiCpuPkg/MtrrLib: Add API MtrrGetMemoryAttributesInMtrrSettings.
  UefiCpuPkg/MtrrLib: Improve MtrrDebugPrintAllMtrrsWorker.
  UefiCpuPkg/MtrrUnitTest: Add Unit test of setting/getting memory
    attributes

 UefiCpuPkg/CpuDxe/CpuDxe.c                            | 309 ++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 UefiCpuPkg/Include/Library/MtrrLib.h                  |  32 +++++++++++++++++++++++++++++---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c                  | 391 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------
 UefiCpuPkg/Library/MtrrLib/UnitTest/MtrrLibUnitTest.c | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------
 UefiCpuPkg/Library/MtrrLib/UnitTest/Support.c         |  24 +++++++++++++++---------
 5 files changed, 532 insertions(+), 468 deletions(-)

--
2.36.1.windows.1








-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#109434): https://edk2.groups.io/g/devel/message/109434
Mute This Topic: https://groups.io/mt/101331015/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2023-10-09  6:27 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-13  4:26 [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 01/16] UefiCpuPkg/MtrrLib: Add internal function MtrrLibIsMtrrSupported Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 02/16] UefiCpuPkg/MtrrUnitTest: Update the Unit Test for IsMtrrSupported() Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 03/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestGetFirmwareVariableMtrrCount Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 04/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetDefaultMemoryType Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 05/16] UefiCpuPkg/MtrrUnitTest: Update test to cover no-fixed-mtrr cases Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 06/16] UefiCpuPkg/MtrrLib: Fix MtrrGetAllMtrrs to return correct MTRR setting Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 07/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetAllMtrrs() Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 08/16] UefiCpuPkg/MtrrLib: Update MtrrGetFixedMtrr() Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 09/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrGetFixedMtrr() Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 10/16] UefiCpuPkg/MtrrLib: Fix MtrrSetAllMtrrs to handle absent fixed MTRRs Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 11/16] UefiCpuPkg/MtrrUnitTest: Update UnitTestMtrrSetAllMtrrs() Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 12/16] UefiCpuPkg/MtrrLib: Update APIs related to set memory attributes Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 13/16] UefiCpuPkg/MtrrLib: Add API MtrrGetMemoryAttributesInMtrrSettings Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 14/16] UefiCpuPkg/MtrrLib: Improve MtrrDebugPrintAllMtrrsWorker Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 15/16] UefiCpuPkg/MtrrUnitTest: Add Unit test of setting/getting memory attributes Yuanhao Xie
2023-09-13  4:26 ` [edk2-devel] [PATCH 16/16] UefiCpuPkg/CpuDxe: Update RefreshMemoryAttributesFromMtrr Yuanhao Xie
2023-10-01  9:30 ` [edk2-devel] [PATCH 00/16] MtrrLib modules and Unit test Enhancement Ni, Ray
2023-10-09  6:27 ` Dong, Eric

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox