public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Yuanhao Xie" <yuanhao.xie@intel.com>
To: devel@edk2.groups.io
Cc: Ray Ni <ray.ni@intel.com>, Eric Dong <eric.dong@intel.com>,
	Rahul Kumar <rahul1.kumar@intel.com>,
	Gerd Hoffmann <kraxel@redhat.com>
Subject: [edk2-devel] [PATCH 13/16] UefiCpuPkg/MtrrLib: Add API MtrrGetMemoryAttributesInMtrrSettings.
Date: Wed, 13 Sep 2023 12:26:36 +0800	[thread overview]
Message-ID: <20230913042639.2066-14-yuanhao.xie@intel.com> (raw)
In-Reply-To: <20230913042639.2066-1-yuanhao.xie@intel.com>

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]
-=-=-=-=-=-=-=-=-=-=-=-



  parent reply	other threads:[~2023-09-13  4:27 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Yuanhao Xie [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230913042639.2066-14-yuanhao.xie@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox