From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 14D9FAC09B3 for ; Wed, 13 Sep 2023 04:27:39 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=Bq3P3qtppQ2nX4ca0tmKwXZpMYQcFnOHo8cSzFItbgE=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1694579258; v=1; b=jD7EAT/tkOxM3ZEdQcJSQjwEU7kBuELjfoxvnPNPvAdnE+i5wXk3OHnsOff6qSP+aRBWHb6X 9CdPG8B24wFu8MC9EfVCwozqadsI4TIgmDVrCqxWDA13nIbvjz29l+UaqiuyC7FqeA0A4nPv3aD U6eEAsnKSslQbF6434Jl70qs= X-Received: by 127.0.0.2 with SMTP id 6L0OYY7687511xEW4ZIOlIbG; Tue, 12 Sep 2023 21:27:38 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.126]) by mx.groups.io with SMTP id smtpd.web11.4645.1694579216558329653 for ; Tue, 12 Sep 2023 21:27:38 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="363595514" X-IronPort-AV: E=Sophos;i="6.02,142,1688454000"; d="scan'208";a="363595514" X-Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Sep 2023 21:27:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="859093696" X-IronPort-AV: E=Sophos;i="6.02,142,1688454000"; d="scan'208";a="859093696" X-Received: from shwdeopenlab705.ccr.corp.intel.com ([10.239.55.100]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Sep 2023 21:27:36 -0700 From: "Yuanhao Xie" To: devel@edk2.groups.io Cc: Ray Ni , Eric Dong , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [PATCH 16/16] UefiCpuPkg/CpuDxe: Update RefreshMemoryAttributesFromMtrr. Date: Wed, 13 Sep 2023 12:26:39 +0800 Message-Id: <20230913042639.2066-17-yuanhao.xie@intel.com> In-Reply-To: <20230913042639.2066-1-yuanhao.xie@intel.com> References: <20230913042639.2066-1-yuanhao.xie@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,yuanhao.xie@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: DOWoXiMtHPGxGy8E5l7tvVP8x7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b="jD7EAT/t"; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io From: Ray Ni 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 Cc: Eric Dong Cc: Rahul Kumar Cc: Gerd Hoffmann --- 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] -=-=-=-=-=-=-=-=-=-=-=-