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 389EDD80F71 for ; Mon, 29 Jan 2024 20:07:33 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=48qEp2am8Lu2v+l5l77P4KKktH0X7kXJXSKrGab8eFs=; c=relaxed/simple; d=groups.io; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From:In-Reply-To:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type:Content-Transfer-Encoding; s=20140610; t=1706558851; v=1; b=tgh2FOx5JSEcxVmaM/2/xyeK8FKi4na2i5psJWYsrno6ZFGKyk9HxzO80CdkWISX3V9jSnlh d2ujFd4Cz62qf5D4UnFqImChOD41MvpE+T/mNuemirg4LDIsc8iplR8jSI2I86+DkJssJ4wxXw5 XQBE3z6jygyPByNrQRH3ZxO8= X-Received: by 127.0.0.2 with SMTP id UsJuYY7687511xgLtgS7yanW; Mon, 29 Jan 2024 12:07:31 -0800 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.groups.io with SMTP id smtpd.web10.759.1706558851009274862 for ; Mon, 29 Jan 2024 12:07:31 -0800 X-Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-662-jz72hHehPH2pS7Jc8ZsyaQ-1; Mon, 29 Jan 2024 15:07:24 -0500 X-MC-Unique: jz72hHehPH2pS7Jc8ZsyaQ-1 X-Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 57C4883FC29; Mon, 29 Jan 2024 20:07:24 +0000 (UTC) X-Received: from [10.39.192.97] (unknown [10.39.192.97]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9EBBA492BC6; Mon, 29 Jan 2024 20:07:22 +0000 (UTC) Message-ID: Date: Mon, 29 Jan 2024 21:07:21 +0100 MIME-Version: 1.0 Subject: Re: [edk2-devel] [Patch v2 2/2] MdeModulePkg/Core/Dxe: Set MemoryTypeInfo bin range from HOB To: devel@edk2.groups.io, michael.d.kinney@intel.com Cc: Liming Gao , Aaron Li , Liu Yun , Andrew Fish References: <20240129034514.1898-1-michael.d.kinney@intel.com> <20240129034514.1898-3-michael.d.kinney@intel.com> From: "Laszlo Ersek" In-Reply-To: <20240129034514.1898-3-michael.d.kinney@intel.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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,lersek@redhat.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: mymrwtTaVl8175D4DhwJWnGux7686176AA= Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=tgh2FOx5; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=redhat.com (policy=none) On 1/29/24 04:45, Michael D Kinney wrote: > Provide an optional method for PEI to declare a specific address > range to use for the Memory Type Information bins. The current > algorithm uses heuristics that tends to place the Memory Type > Information bins in the same location, but memory configuration > changes across boots or algorithm changes across a firmware > updates could potentially change the Memory Type Information bin > location. >=20 > If the HOB List contains a Resource Descriptor HOB that > describes tested system memory and has an Owner GUID of > gEfiMemoryTypeInformationGuid, then use the address range > described by the Resource Descriptor HOB as the preferred > location of the Memory Type Information bins. If this HOB is > not detected, then the current behavior is preserved. >=20 > The HOB with an Owner GUID of gEfiMemoryTypeInformationGuid > is ignored for the following conditions: > * The HOB with an Owner GUID of gEfiMemoryTypeInformationGuid > is smaller than the Memory Type Information bins. > * The HOB list contains more than one Resource Descriptor HOB > with an owner GUID of gEfiMemoryTypeInformationGuid. > * The Resource Descriptor HOB with an Owner GUID of > gEfiMemoryTypeInformationGuid is the same Resource Descriptor > HOB that that describes the PHIT memory range. >=20 > Cc: Liming Gao > Cc: Aaron Li > Cc: Liu Yun > Cc: Andrew Fish > Signed-off-by: Michael D Kinney > --- > MdeModulePkg/Core/Dxe/DxeMain.h | 6 ++ > MdeModulePkg/Core/Dxe/Gcd/Gcd.c | 72 ++++++++++++- > MdeModulePkg/Core/Dxe/Mem/Page.c | 101 ++++++++++++++++++ > .../Include/Guid/MemoryTypeInformation.h | 14 ++- > 4 files changed, 185 insertions(+), 8 deletions(-) >=20 > diff --git a/MdeModulePkg/Core/Dxe/DxeMain.h b/MdeModulePkg/Core/Dxe/DxeM= ain.h > index 86a7be2f5188..53e26703f8c7 100644 > --- a/MdeModulePkg/Core/Dxe/DxeMain.h > +++ b/MdeModulePkg/Core/Dxe/DxeMain.h > @@ -277,6 +277,12 @@ CoreInitializePool ( > VOID > ); > =20 > +VOID > +CoreSetMemoryTypeInformationRange ( > + IN EFI_PHYSICAL_ADDRESS Start, > + IN UINT64 Length > + ); > + > /** > Called to initialize the memory map and add descriptors to > the current descriptor list. > diff --git a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c b/MdeModulePkg/Core/Dxe/Gcd/= Gcd.c > index 792cd2e0af23..c450d1bf2531 100644 > --- a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c > +++ b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c > @@ -2238,6 +2238,8 @@ CoreInitializeMemoryServices ( > EFI_HOB_HANDOFF_INFO_TABLE *PhitHob; > EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob; > EFI_HOB_RESOURCE_DESCRIPTOR *PhitResourceHob; > + EFI_HOB_RESOURCE_DESCRIPTOR *MemoryTypeInformationResourceHob; > + UINTN Count; > EFI_PHYSICAL_ADDRESS BaseAddress; > UINT64 Length; > UINT64 Attributes; > @@ -2289,12 +2291,47 @@ CoreInitializeMemoryServices ( > // > // See if a Memory Type Information HOB is available > // > - GuidHob =3D GetFirstGuidHob (&gEfiMemoryTypeInformationGuid); > + MemoryTypeInformationResourceHob =3D NULL; > + GuidHob =3D GetFirstGuidHob (&gEfiMemoryTypeI= nformationGuid); > if (GuidHob !=3D NULL) { > EfiMemoryTypeInformation =3D GET_GUID_HOB_DATA (GuidHob); > DataSize =3D GET_GUID_HOB_DATA_SIZE (GuidHob); > if ((EfiMemoryTypeInformation !=3D NULL) && (DataSize > 0) && (DataS= ize <=3D (EfiMaxMemoryType + 1) * sizeof (EFI_MEMORY_TYPE_INFORMATION))) { > CopyMem (&gMemoryTypeInformation, EfiMemoryTypeInformation, DataSi= ze); > + > + // > + // Look for Resource Descriptor HOB with a ResourceType of System = Memory > + // and an Owner GUID of gEfiMemoryTypeInformationGuid. If more tha= n 1 is > + // found, then set MemoryTypeInformationResourceHob to NULL. > + // > + Count =3D 0; > + for (Hob.Raw =3D *HobStart; !END_OF_HOB_LIST (Hob); Hob.Raw =3D GE= T_NEXT_HOB (Hob)) { > + if (GET_HOB_TYPE (Hob) !=3D EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) { > + continue; > + } > + > + ResourceHob =3D Hob.ResourceDescriptor; > + if (!CompareGuid (&ResourceHob->Owner, &gEfiMemoryTypeInformatio= nGuid)) { > + continue; > + } > + > + Count++; > + if (ResourceHob->ResourceType !=3D EFI_RESOURCE_SYSTEM_MEMORY) { > + continue; > + } > + > + if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) != =3D TESTED_MEMORY_ATTRIBUTES) { > + continue; > + } > + > + if (ResourceHob->ResourceLength >=3D CalculateTotalMemoryBinSize= Needed ()) { > + MemoryTypeInformationResourceHob =3D ResourceHob; > + } > + } > + > + if (Count > 1) { > + MemoryTypeInformationResourceHob =3D NULL; > + } > } > } > =20 > @@ -2344,6 +2381,15 @@ CoreInitializeMemoryServices ( > PhitResourceHob =3D ResourceHob; > Found =3D TRUE; > =20 > + // > + // If a Memory Type Information Resource HOB was found and is the sa= me > + // Resource HOB that describes the PHIT HOB, then ignore the Memory = Type > + // Information Resource HOB. > + // > + if (MemoryTypeInformationResourceHob =3D=3D PhitResourceHob) { > + MemoryTypeInformationResourceHob =3D NULL; > + } > + > // > // Compute range between PHIT EfiMemoryTop and the end of the Resour= ce Descriptor HOB > // > @@ -2387,8 +2433,9 @@ CoreInitializeMemoryServices ( > if (Length < MinimalMemorySizeNeeded) { > // > // Search all the resource descriptor HOBs from the highest possible= addresses down for a memory > - // region that is big enough to initialize the DXE core. Always ski= p the PHIT Resource HOB. > - // The max address must be within the physically addressible range f= or the processor. > + // region that is big enough to initialize the DXE core. Always ski= p the PHIT Resource HOB > + // and the Memory Type Information Resource HOB. The max address mus= t be within the physically > + // addressable range for the processor. > // > HighAddress =3D MAX_ALLOC_ADDRESS; > for (Hob.Raw =3D *HobStart; !END_OF_HOB_LIST (Hob); Hob.Raw =3D GET_= NEXT_HOB (Hob)) { > @@ -2399,6 +2446,13 @@ CoreInitializeMemoryServices ( > continue; > } > =20 > + // > + // Skip the Resource Descriptor HOB that contains Memory Type Info= rmation bins > + // > + if (Hob.ResourceDescriptor =3D=3D MemoryTypeInformationResourceHob= ) { > + continue; > + } > + > // > // Skip all HOBs except Resource Descriptor HOBs > // > @@ -2466,6 +2520,18 @@ CoreInitializeMemoryServices ( > Capabilities =3D CoreConvertResourceDescriptorHobAttributesToCapabil= ities (EfiGcdMemoryTypeSystemMemory, Attributes); > } > =20 > + if (MemoryTypeInformationResourceHob !=3D NULL) { > + // > + // If a Memory Type Information Resource HOB was found, then use the= address > + // range of the Memory Type Information Resource HOB as the preferr= ed > + // address range for the Memory Type Information bins. > + // > + CoreSetMemoryTypeInformationRange ( > + MemoryTypeInformationResourceHob->PhysicalStart, > + MemoryTypeInformationResourceHob->ResourceLength > + ); > + } > + > // > // Declare the very first memory region, so the EFI Memory Services ar= e available. > // > diff --git a/MdeModulePkg/Core/Dxe/Mem/Page.c b/MdeModulePkg/Core/Dxe/Mem= /Page.c > index 6497af573353..458c62090265 100644 > --- a/MdeModulePkg/Core/Dxe/Mem/Page.c > +++ b/MdeModulePkg/Core/Dxe/Mem/Page.c > @@ -532,6 +532,107 @@ CoreLoadingFixedAddressHook ( > return; > } > =20 > +/** > + Sets the preferred memory range to use for the Memory Type Information= bins. > + This service must be called before fist call to CoreAddMemoryDescripto= r(). > + > + If the location of the Memory Type Information bins has already been > + established or the size of the range provides is smaller than all the > + Memory Type Information bins, then the range provides is not used. > + > + @param Start The start address of the Memory Type Information range= . > + @param Length The size, in bytes, of the Memory Type Information ran= ge. > +**/ > +VOID > +CoreSetMemoryTypeInformationRange ( > + IN EFI_PHYSICAL_ADDRESS Start, > + IN UINT64 Length > + ) > +{ > + EFI_PHYSICAL_ADDRESS Top; > + EFI_MEMORY_TYPE Type; > + UINTN Index; > + UINTN Size; > + > + // > + // Return if Memory Type Information bin locations have already been s= et > + // > + if (mMemoryTypeInformationInitialized) { > + return; > + } > + > + // > + // Return if size of the Memory Type Information bins is greater than = Length > + // > + Size =3D 0; > + for (Index =3D 0; gMemoryTypeInformation[Index].Type !=3D EfiMaxMemory= Type; Index++) { > + // > + // Make sure the memory type in the gMemoryTypeInformation[] array i= s valid > + // > + Type =3D (EFI_MEMORY_TYPE)(gMemoryTypeInformation[Index].Type); > + if ((UINT32)Type > EfiMaxMemoryType) { > + continue; > + } > + > + Size +=3D EFI_PAGES_TO_SIZE (gMemoryTypeInformation[Index].NumberOfP= ages); > + } > + > + if (Size > Length) { > + return; > + } > + > + // > + // Loop through each memory type in the order specified by the > + // gMemoryTypeInformation[] array > + // > + Top =3D Start + Length; > + for (Index =3D 0; gMemoryTypeInformation[Index].Type !=3D EfiMaxMemory= Type; Index++) { > + // > + // Make sure the memory type in the gMemoryTypeInformation[] array i= s valid > + // > + Type =3D (EFI_MEMORY_TYPE)(gMemoryTypeInformation[Index].Type); > + if ((UINT32)Type > EfiMaxMemoryType) { > + continue; > + } > + > + if (gMemoryTypeInformation[Index].NumberOfPages !=3D 0) { > + mMemoryTypeStatistics[Type].MaximumAddress =3D Top - 1; > + Top -=3D EFI_PAGES_TO_SIZE (= gMemoryTypeInformation[Index].NumberOfPages); > + mMemoryTypeStatistics[Type].BaseAddress =3D Top; > + > + // > + // If the current base address is the lowest address so far, then = update > + // the default maximum address > + // > + if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddre= ss) { > + mDefaultMaximumAddress =3D mMemoryTypeStatistics[Type].BaseAddre= ss - 1; > + } > + > + mMemoryTypeStatistics[Type].NumberOfPages =3D gMemoryTypeInforma= tion[Index].NumberOfPages; > + gMemoryTypeInformation[Index].NumberOfPages =3D 0; > + } > + } > + > + // > + // If the number of pages reserved for a memory type is 0, then all > + // allocations for that type should be in the default range. > + // > + for (Type =3D (EFI_MEMORY_TYPE)0; Type < EfiMaxMemoryType; Type++) { > + for (Index =3D 0; gMemoryTypeInformation[Index].Type !=3D EfiMaxMemo= ryType; Index++) { > + if (Type =3D=3D (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Typ= e) { > + mMemoryTypeStatistics[Type].InformationIndex =3D Index; > + } > + } > + > + mMemoryTypeStatistics[Type].CurrentNumberOfPages =3D 0; > + if (mMemoryTypeStatistics[Type].MaximumAddress =3D=3D MAX_ALLOC_ADDR= ESS) { > + mMemoryTypeStatistics[Type].MaximumAddress =3D mDefaultMaximumAddr= ess; > + } > + } > + > + mMemoryTypeInformationInitialized =3D TRUE; > +} > + > /** > Called to initialize the memory map and add descriptors to > the current descriptor list. > diff --git a/MdeModulePkg/Include/Guid/MemoryTypeInformation.h b/MdeModul= ePkg/Include/Guid/MemoryTypeInformation.h > index e97d88765ece..65adcf478b80 100644 > --- a/MdeModulePkg/Include/Guid/MemoryTypeInformation.h > +++ b/MdeModulePkg/Include/Guid/MemoryTypeInformation.h > @@ -1,14 +1,18 @@ > /** @file > This file defines: > - * Memory Type Information GUID for HOB and Variable. > + * Memory Type Information GUID for Guided HOB and Variable. > * Memory Type Information Variable Name. > * Memory Type Information GUID HOB data structure. > =20 > - The memory type information HOB and variable can > - be used to store the information for each memory type in Variable or H= OB. > + The memory type information HOB and variable can be used to store info= rmation > + for each memory type in Variable or HOB. > =20 > -Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
> -SPDX-License-Identifier: BSD-2-Clause-Patent > + The Memory Type Information GUID can also be optionally used as the Ow= ner > + field of a Resource Descriptor HOB to provide the preferred memory ran= ge > + for the memory types described in the Memory Type Information GUID HOB= . > + > + Copyright (c) 2006, Intel Corporation. All rights reserved.
> + SPDX-License-Identifier: BSD-2-Clause-Patent > =20 > **/ > =20 Thanks. I've not checked everything in minute detail, but I've made a reasonable effort to review this. Some comments (no deal breaker though): - the "Count" counting is a bit stricter than truly necessary. I'd expect the Count++ to be exactly where the "MemoryTypeInformationResourceHob =3D ResourceHob" assignment is as well. However, the posted version is also fine; it's fine to be stricter than necessary. (And the code matches the commit message.) - the early return point under "locations have already been set" didn't get a DEBUG_ERROR after all, but that's not a big deal either - I find it a bit confusing that we trust the gMemoryTypeInformation array to be terminated with Type=3D=3DEfiMaxMemoryType, but we still need t= o / want to skip any elements with Type > EfiMaxMemoryType. If I trusted the array to be terminated properly, I'd also trust it not to contain entries with types that are even beyond the terminator type. Near the end of that function, we also trust the Type fields in gMemoryTypeInformation to be unique. But, still, more checking cannot hurt. Reviewed-by: Laszlo Ersek -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#114742): https://edk2.groups.io/g/devel/message/114742 Mute This Topic: https://groups.io/mt/104025663/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/leave/12367111/7686176/19134562= 12/xyzzy [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-