]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c
MdePkg PeiMemoryAllocationLib: Update InternalAllocateAlignedPages
[mirror_edk2.git] / MdePkg / Library / PeiMemoryAllocationLib / MemoryAllocationLib.c
index 90d4bbfd4941cf9b9c2d1fdc009f3a678c4ce055..5cd24a38a3c66c6526618ee326453897a267fdab 100644 (file)
@@ -2,7 +2,7 @@
   Support routines for memory allocation routines \r
   based on PeiService for PEI phase drivers.\r
 \r
-  Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials                          \r
   are licensed and made available under the terms and conditions of the BSD License         \r
   which accompanies this distribution.  The full text of the license may be found at        \r
@@ -45,42 +45,15 @@ InternalAllocatePages (
 {\r
   EFI_STATUS            Status;\r
   EFI_PHYSICAL_ADDRESS  Memory; \r
-  EFI_MEMORY_TYPE       RequestType;\r
-  EFI_PEI_HOB_POINTERS  Hob;\r
 \r
   if (Pages == 0) {\r
     return NULL;\r
   }\r
 \r
-  RequestType = MemoryType;\r
-  if (MemoryType == EfiReservedMemoryType) {\r
-    //\r
-    // PEI AllocatePages() doesn't support EfiReservedMemoryType. \r
-    // Change RequestType to EfiBootServicesData for memory allocation.\r
-    //\r
-    RequestType = EfiBootServicesData;\r
-  }\r
-\r
-  Status = PeiServicesAllocatePages (RequestType, Pages, &Memory);\r
+  Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory);\r
   if (EFI_ERROR (Status)) {\r
     return NULL;\r
   }\r
-  \r
-  if (MemoryType == EfiReservedMemoryType) {\r
-    //\r
-    // Memory type needs to be updated to EfiReservedMemoryType. Per PI spec Volume 1, \r
-    // PEI AllocatePages() will automate the creation of the Memory Allocation HOB types. \r
-    // Search Memory Allocation HOB and find the matched memory region,\r
-    // then change its memory type to EfiReservedMemoryType.\r
-    //\r
-    Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
-    while (Hob.Raw != NULL && Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress != Memory) {\r
-      Hob.Raw = GET_NEXT_HOB (Hob);\r
-      Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
-    }\r
-    ASSERT (Hob.Raw != NULL);\r
-    Hob.MemoryAllocation->AllocDescriptor.MemoryType = EfiReservedMemoryType;\r
-  }\r
 \r
   return (VOID *) (UINTN) Memory;\r
 }\r
@@ -175,10 +148,11 @@ FreePages (
   IN UINTN  Pages\r
   )\r
 {\r
+  EFI_STATUS  Status;\r
+\r
   ASSERT (Pages != 0);\r
-  //\r
-  // PEI phase does not support to free pages, so leave it as NOP.\r
-  //\r
+  Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
 /**\r
@@ -189,6 +163,7 @@ FreePages (
   If there is not enough memory at the specified alignment remaining to satisfy the request, then\r
   NULL is returned.\r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  MemoryType            The type of memory to allocate.\r
   @param  Pages                 The number of 4 KB pages to allocate.\r
@@ -206,8 +181,12 @@ InternalAllocateAlignedPages (
   IN UINTN            Alignment\r
   )\r
 {\r
-  VOID    *Memory;\r
-  UINTN   AlignmentMask;\r
+  EFI_STATUS            Status;\r
+  EFI_PHYSICAL_ADDRESS  Memory;\r
+  UINTN                 AlignedMemory;\r
+  UINTN                 AlignmentMask;\r
+  UINTN                 UnalignedPages;\r
+  UINTN                 RealPages;\r
 \r
   //\r
   // Alignment must be a power of two or zero.\r
@@ -217,20 +196,50 @@ InternalAllocateAlignedPages (
   if (Pages == 0) {\r
     return NULL;\r
   }\r
-  //\r
-  // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
-  //\r
-  ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r
-  //\r
-  // We would rather waste some memory to save PEI code size.\r
-  //\r
-  Memory = InternalAllocatePages (MemoryType, Pages + EFI_SIZE_TO_PAGES (Alignment));\r
-  if (Alignment == 0) {\r
-    AlignmentMask = Alignment;\r
+  if (Alignment > EFI_PAGE_SIZE) {\r
+    //\r
+    // Calculate the total number of pages since alignment is larger than page size.\r
+    //\r
+    AlignmentMask  = Alignment - 1;\r
+    RealPages      = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
+    //\r
+    // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
+    //\r
+    ASSERT (RealPages > Pages);\r
+\r
+    Status         = PeiServicesAllocatePages (MemoryType, RealPages, &Memory);\r
+    if (EFI_ERROR (Status)) {\r
+      return NULL;\r
+    }\r
+    AlignedMemory  = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
+    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);\r
+    if (UnalignedPages > 0) {\r
+      //\r
+      // Free first unaligned page(s).\r
+      //\r
+      Status = PeiServicesFreePages (Memory, UnalignedPages);\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+    Memory         = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
+    UnalignedPages = RealPages - Pages - UnalignedPages;\r
+    if (UnalignedPages > 0) {\r
+      //\r
+      // Free last unaligned page(s).\r
+      //\r
+      Status = PeiServicesFreePages (Memory, UnalignedPages);\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
   } else {\r
-    AlignmentMask = Alignment - 1;  \r
+    //\r
+    // Do not over-allocate pages in this case.\r
+    //\r
+    Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory);\r
+    if (EFI_ERROR (Status)) {\r
+      return NULL;\r
+    }\r
+    AlignedMemory  = (UINTN) Memory;\r
   }\r
-  return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);\r
+  return (VOID *) AlignedMemory;\r
 }\r
 \r
 /**\r
@@ -242,6 +251,7 @@ InternalAllocateAlignedPages (
   request, then NULL is returned.\r
   \r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  Pages                 The number of 4 KB pages to allocate.\r
   @param  Alignment             The requested alignment of the allocation.  \r
@@ -270,6 +280,7 @@ AllocateAlignedPages (
   request, then NULL is returned.\r
   \r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  Pages                 The number of 4 KB pages to allocate.\r
   @param  Alignment             The requested alignment of the allocation.  \r
@@ -298,6 +309,7 @@ AllocateAlignedRuntimePages (
   request, then NULL is returned.\r
   \r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  Pages                 The number of 4 KB pages to allocate.\r
   @param  Alignment             The requested alignment of the allocation.  \r
@@ -341,10 +353,11 @@ FreeAlignedPages (
   IN UINTN  Pages\r
   )\r
 {\r
+  EFI_STATUS  Status;\r
+\r
   ASSERT (Pages != 0);\r
-  //\r
-  // PEI phase does not support to free pages, so leave it as NOP.\r
-  //\r
+  Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
 /**\r