public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Zeng, Star" <star.zeng@intel.com>
To: Ard Biesheuvel <ard.biesheuvel@linaro.org>,
	"edk2-devel@lists.01.org" <edk2-devel@lists.01.org>,
	"Gao, Liming" <liming.gao@intel.com>
Cc: "Zeng, Star" <star.zeng@intel.com>
Subject: Re: [PATCH] MdeModulePkg/PeiCore: honour minimal runtime allocation granularity
Date: Fri, 3 Mar 2017 05:08:05 +0000	[thread overview]
Message-ID: <0C09AFA07DD0434D9E2A0C6AEB0483103B82E6FF@shsmsx102.ccr.corp.intel.com> (raw)
In-Reply-To: <1488465136-9905-1-git-send-email-ard.biesheuvel@linaro.org>

Ard,

The code line below also needs to be considered.

if (RemainingPages < Pages) {

The Pages needs to be converted to granularity aligned before the if statement, like the code below in DXE phase.

  NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;
  NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);


Thanks,
Star
-----Original Message-----
From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Ard Biesheuvel
Sent: Thursday, March 2, 2017 10:32 PM
To: edk2-devel@lists.01.org; Gao, Liming <liming.gao@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Subject: [edk2] [PATCH] MdeModulePkg/PeiCore: honour minimal runtime allocation granularity

Architectures such as AArch64 may run the OS with 16 KB or 64 KB sized pages, and for this reason, the UEFI spec mandates a minimal allocation granularity of 64 KB for regions that may require different memory attributes at OS runtime.

So make PeiCore's implementation of AllocatePages () take this into account as well.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 MdeModulePkg/Core/Pei/Memory/MemoryServices.c | 20 ++++++++++++++++----
 MdeModulePkg/Core/Pei/PeiMain.h               | 18 ++++++++++++++++++
 2 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
index 4efe14313ca5..201904e415b9 100644
--- a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
+++ b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
@@ -140,6 +140,7 @@ PeiAllocatePages (
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryTop;
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryBottom;
   UINTN                                   RemainingPages;
+  UINTN                                   Granularity;
 
   if ((MemoryType != EfiLoaderCode) &&
       (MemoryType != EfiLoaderData) &&
@@ -153,6 +154,16 @@ PeiAllocatePages (
     return EFI_INVALID_PARAMETER;
   }
 
+  Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
+
+  if  (MemoryType == EfiACPIReclaimMemory   ||
+       MemoryType == EfiACPIMemoryNVS       ||
+       MemoryType == EfiRuntimeServicesCode ||
+       MemoryType == EfiRuntimeServicesData) {
+
+    Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
+  }
+
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
   Hob.Raw     = PrivateData->HobList.Raw;
   
@@ -176,9 +187,10 @@ PeiAllocatePages (
   }
 
   //
-  // Check to see if on 4k boundary, If not aligned, make the allocation aligned.
+  // Check to see if on correct boundary for the memory type.
+  // If not aligned, make the allocation aligned.
   //
-  *(FreeMemoryTop) -= *(FreeMemoryTop) & 0xFFF;
+  *(FreeMemoryTop) -= *(FreeMemoryTop) & (Granularity - 1);
 
   //
   // Verify that there is sufficient memory to satisfy the allocation.
@@ -200,7 +212,7 @@ PeiAllocatePages (
     //
     // Update the PHIT to reflect the memory usage
     //
-    *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;
+    *(FreeMemoryTop) -= ALIGN_VALUE (Pages * EFI_PAGE_SIZE, 
+ Granularity);
 
     //
     // Update the value for the caller
@@ -212,7 +224,7 @@ PeiAllocatePages (
     //
     BuildMemoryAllocationHob (
       *(FreeMemoryTop),
-      Pages * EFI_PAGE_SIZE,
+      ALIGN_VALUE (Pages * EFI_PAGE_SIZE, Granularity),
       MemoryType
       );
 
diff --git a/MdeModulePkg/Core/Pei/PeiMain.h b/MdeModulePkg/Core/Pei/PeiMain.h index 69eea514920b..e8358d3c4e6d 100644
--- a/MdeModulePkg/Core/Pei/PeiMain.h
+++ b/MdeModulePkg/Core/Pei/PeiMain.h
@@ -55,6 +55,24 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 ///
 #define PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE   0xff
 
+#if defined (MDE_CPU_AARCH64)
+///
+/// 64-bit ARM systems allow the OS to execute with 64 KB page size, 
+/// so for improved interoperability with the firmware, align the /// 
+runtime regions to 64 KB as well ///
+#define RUNTIME_PAGE_ALLOCATION_GRANULARITY       (SIZE_64KB)
+#define DEFAULT_PAGE_ALLOCATION_GRANULARITY       (EFI_PAGE_SIZE)
+
+#else
+///
+/// For generic EFI machines make the default allocations 4K aligned 
+///
+#define RUNTIME_PAGE_ALLOCATION_GRANULARITY       (EFI_PAGE_SIZE)
+#define DEFAULT_PAGE_ALLOCATION_GRANULARITY       (EFI_PAGE_SIZE)
+
+#endif
+
 ///
 /// Pei Core private data structures
 ///
--
2.7.4

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


      reply	other threads:[~2017-03-03  5:08 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-02 14:32 [PATCH] MdeModulePkg/PeiCore: honour minimal runtime allocation granularity Ard Biesheuvel
2017-03-03  5:08 ` Zeng, Star [this message]

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=0C09AFA07DD0434D9E2A0C6AEB0483103B82E6FF@shsmsx102.ccr.corp.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