]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/Memory/MemoryServices.c
MdeModulePkg/PeiCore: honour minimal runtime allocation granularity
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
index 58c5d81517ea30884c442c590b5c987781ee3161..719372e0612801176d6bd47d68c0c12b791b69f0 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   EFI PEI Core memory services\r
   \r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
+Copyright (c) 2006 - 2015, 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
 http://opensource.org/licenses/bsd-license.php                                            \r
@@ -12,14 +12,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <PeiMain.h>\r
+#include "PeiMain.h"\r
 \r
 /**\r
 \r
   Initialize the memory services.\r
 \r
-\r
-  @param PrivateData     Add parameter description\r
+  @param PrivateData     Points to PeiCore's private instance data.\r
   @param SecCoreData     Points to a data structure containing information about the PEI core's operating\r
                          environment, such as the size and location of temporary RAM, the stack location and\r
                          the BFV location.\r
@@ -35,36 +34,27 @@ InitializeMemoryServices (
   )\r
 {\r
   \r
-  PrivateData->SwitchStackSignal      = FALSE;\r
+  PrivateData->SwitchStackSignal    = FALSE;\r
 \r
+  //\r
+  // First entering PeiCore, following code will initialized some field\r
+  // in PeiCore's private data according to hand off data from sec core.\r
+  //\r
   if (OldCoreData == NULL) {\r
 \r
     PrivateData->PeiMemoryInstalled = FALSE;\r
-\r
-    PrivateData->BottomOfCarHeap        = SecCoreData->PeiTemporaryRamBase; \r
-    PrivateData->TopOfCarHeap           = (VOID *)((UINTN)(PrivateData->BottomOfCarHeap) + SecCoreData->PeiTemporaryRamSize);\r
-    PrivateData->SizeOfTemporaryMemory  = SecCoreData->TemporaryRamSize;\r
-    PrivateData->StackSize              = (UINT64) SecCoreData->StackSize;\r
-    \r
-    DEBUG_CODE_BEGIN ();\r
-      PrivateData->SizeOfCacheAsRam = SecCoreData->PeiTemporaryRamSize + SecCoreData->StackSize;\r
-      PrivateData->MaxTopOfCarHeap  = (VOID *) ((UINTN) PrivateData->BottomOfCarHeap + (UINTN) PrivateData->SizeOfCacheAsRam);\r
-      PrivateData->StackBase        = (EFI_PHYSICAL_ADDRESS) (UINTN) SecCoreData->StackBase;\r
-      PrivateData->StackSize        = (UINT64) SecCoreData->StackSize;\r
-    DEBUG_CODE_END ();\r
-\r
-    PrivateData->HobList.Raw = PrivateData->BottomOfCarHeap;\r
+    PrivateData->HobList.Raw        = SecCoreData->PeiTemporaryRamBase;\r
     \r
     PeiCoreBuildHobHandoffInfoTable (\r
       BOOT_WITH_FULL_CONFIGURATION,\r
-      (EFI_PHYSICAL_ADDRESS) (UINTN) PrivateData->BottomOfCarHeap,\r
+      (EFI_PHYSICAL_ADDRESS) (UINTN) SecCoreData->PeiTemporaryRamBase,\r
       (UINTN) SecCoreData->PeiTemporaryRamSize\r
       );\r
 \r
     //\r
-    // Set PS to point to ServiceTableShadow in Cache\r
+    // Set Ps to point to ServiceTableShadow in Cache\r
     //\r
-    PrivateData->PS = &(PrivateData->ServiceTableShadow);\r
+    PrivateData->Ps = &(PrivateData->ServiceTableShadow);\r
   }\r
   \r
   return;\r
@@ -72,12 +62,16 @@ InitializeMemoryServices (
 \r
 /**\r
 \r
-  Install the permanent memory is now available.\r
-  Creates HOB (PHIT and Stack).\r
+  This function registers the found memory configuration with the PEI Foundation.\r
 \r
-  @param PeiServices     - The PEI core services table.\r
-  @param MemoryBegin     - Start of memory address.\r
-  @param MemoryLength    - Length of memory.\r
+  The usage model is that the PEIM that discovers the permanent memory shall invoke this service.\r
+  This routine will hold discoveried memory information into PeiCore's private data,\r
+  and set SwitchStackSignal flag. After PEIM who discovery memory is dispatched,\r
+  PeiDispatcher will migrate temporary memory to permenement memory.\r
+  \r
+  @param PeiServices        An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param MemoryBegin        Start of memory address.\r
+  @param MemoryLength       Length of memory.\r
 \r
   @return EFI_SUCCESS Always success.\r
 \r
@@ -95,6 +89,17 @@ PeiInstallPeiMemory (
   DEBUG ((EFI_D_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));\r
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
 \r
+  //\r
+  // PEI_SERVICE.InstallPeiMemory should only be called one time during whole PEI phase.\r
+  // If it is invoked more than one time, ASSERT information is given for developer debugging in debug tip and\r
+  // simply return EFI_SUCESS in release tip to ignore it.\r
+  // \r
+  if (PrivateData->PeiMemoryInstalled) {\r
+    DEBUG ((EFI_D_ERROR, "ERROR: PeiInstallPeiMemory is called more than once!\n"));\r
+    ASSERT (FALSE);\r
+    return EFI_SUCCESS;\r
+  }\r
+  \r
   PrivateData->PhysicalMemoryBegin   = MemoryBegin;\r
   PrivateData->PhysicalMemoryLength  = MemoryLength;\r
   PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;\r
@@ -105,37 +110,64 @@ PeiInstallPeiMemory (
 }\r
 \r
 /**\r
+  The purpose of the service is to publish an interface that allows \r
+  PEIMs to allocate memory ranges that are managed by the PEI Foundation.\r
 \r
-  Memory allocation service on permanent memory,\r
-  not usable prior to the memory installation.\r
-\r
+  @param  PeiServices      An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param  MemoryType       The type of memory to allocate.\r
+  @param  Pages            The number of contiguous 4 KB pages to allocate.\r
+  @param  Memory           Pointer to a physical address. On output, the address is set to the base \r
+                           of the page range that was allocated.\r
 \r
-  @param PeiServices     - The PEI core services table.\r
-  @param MemoryType      - Type of memory to allocate.\r
-  @param Pages           - Number of pages to allocate.\r
-  @param Memory          - Pointer of memory allocated.\r
-\r
-  @retval EFI_SUCCESS              The allocation was successful\r
-  @retval EFI_INVALID_PARAMETER    Only AllocateAnyAddress is supported.\r
-  @retval EFI_NOT_AVAILABLE_YET    Called with permanent memory not available\r
-  @retval EFI_OUT_OF_RESOURCES     There is not enough HOB heap to satisfy the requirement\r
-                                   to allocate the number of pages.\r
+  @retval EFI_SUCCESS           The memory range was successfully allocated.\r
+  @retval EFI_OUT_OF_RESOURCES  The pages could not be allocated.\r
+  @retval EFI_INVALID_PARAMETER Type is not equal to EfiLoaderCode, EfiLoaderData, EfiRuntimeServicesCode, \r
+                                EfiRuntimeServicesData, EfiBootServicesCode, EfiBootServicesData,\r
+                                EfiACPIReclaimMemory, EfiReservedMemoryType, or EfiACPIMemoryNVS.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiAllocatePages (\r
-  IN CONST EFI_PEI_SERVICES           **PeiServices,\r
-  IN EFI_MEMORY_TYPE            MemoryType,\r
-  IN UINTN                      Pages,\r
-  OUT EFI_PHYSICAL_ADDRESS      *Memory\r
+  IN CONST EFI_PEI_SERVICES     **PeiServices,\r
+  IN       EFI_MEMORY_TYPE      MemoryType,\r
+  IN       UINTN                Pages,\r
+  OUT      EFI_PHYSICAL_ADDRESS *Memory\r
   )\r
 {\r
   PEI_CORE_INSTANCE                       *PrivateData;\r
   EFI_PEI_HOB_POINTERS                    Hob;\r
-  EFI_PHYSICAL_ADDRESS                    Offset;\r
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryTop;\r
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryBottom;\r
+  UINTN                                   RemainingPages;\r
+  UINTN                                   Granularity;\r
+  UINTN                                   Padding;\r
+\r
+  if ((MemoryType != EfiLoaderCode) &&\r
+      (MemoryType != EfiLoaderData) &&\r
+      (MemoryType != EfiRuntimeServicesCode) &&\r
+      (MemoryType != EfiRuntimeServicesData) &&\r
+      (MemoryType != EfiBootServicesCode) &&\r
+      (MemoryType != EfiBootServicesData) &&\r
+      (MemoryType != EfiACPIReclaimMemory) &&\r
+      (MemoryType != EfiReservedMemoryType) &&\r
+      (MemoryType != EfiACPIMemoryNVS)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
+\r
+  if  (RUNTIME_PAGE_ALLOCATION_GRANULARITY > DEFAULT_PAGE_ALLOCATION_GRANULARITY &&\r
+       (MemoryType == EfiACPIReclaimMemory   ||\r
+        MemoryType == EfiACPIMemoryNVS       ||\r
+        MemoryType == EfiRuntimeServicesCode ||\r
+        MemoryType == EfiRuntimeServicesData)) {\r
+\r
+    Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
+\r
+    DEBUG ((DEBUG_INFO, "AllocatePages: aligning allocation to %d KB\n",\r
+      Granularity / SIZE_1KB));\r
+  }\r
 \r
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
   Hob.Raw     = PrivateData->HobList.Raw;\r
@@ -145,8 +177,8 @@ PeiAllocatePages (
   //\r
   if (!PrivateData->PeiMemoryInstalled) {\r
     //\r
-    // When PeiInstallMemory is called but CAR has *not* been moved to temporary memory,\r
-    // the AllocatePage will dependent the field of PEI_CORE_INSTANCE structure.\r
+    // When PeiInstallMemory is called but temporary memory has *not* been moved to temporary memory,\r
+    // the AllocatePage will depend on the field of PEI_CORE_INSTANCE structure.\r
     //\r
     if (!PrivateData->SwitchStackSignal) {\r
       return EFI_NOT_AVAILABLE_YET;\r
@@ -159,28 +191,45 @@ PeiAllocatePages (
     FreeMemoryBottom  = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r
   }\r
 \r
-  \r
-\r
   //\r
-  // Check to see if on 4k boundary\r
+  // Check to see if on correct boundary for the memory type.\r
+  // If not aligned, make the allocation aligned.\r
   //\r
-  Offset = *(FreeMemoryTop) & 0xFFF;\r
-  \r
+  Padding = *(FreeMemoryTop) & (Granularity - 1);\r
+  if ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom) < Padding) {\r
+    DEBUG ((DEBUG_ERROR, "AllocatePages failed: Out of space after padding.\n"));\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  *(FreeMemoryTop) -= Padding;\r
+  if (Padding >= EFI_PAGE_SIZE) {\r
+    //\r
+    // Create a memory allocation HOB to cover\r
+    // the pages that we will lose to rounding\r
+    //\r
+    BuildMemoryAllocationHob (\r
+      *(FreeMemoryTop),\r
+      Padding & ~(UINTN)EFI_PAGE_MASK,\r
+      EfiConventionalMemory\r
+      );\r
+  }\r
+\r
   //\r
-  // If not aligned, make the allocation aligned.\r
+  // Verify that there is sufficient memory to satisfy the allocation.\r
+  // For page allocation, the overhead sizeof (EFI_HOB_MEMORY_ALLOCATION) needs to be considered.\r
   //\r
-  if (Offset != 0) {\r
-    *(FreeMemoryTop) -= Offset;\r
+  if ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom) < (UINTN) ALIGN_VALUE (sizeof (EFI_HOB_MEMORY_ALLOCATION), 8)) {\r
+    DEBUG ((EFI_D_ERROR, "AllocatePages failed: No space to build memory allocation hob.\n"));\r
+    return  EFI_OUT_OF_RESOURCES;\r
   }\r
-  \r
+  RemainingPages = (UINTN)(*FreeMemoryTop - *FreeMemoryBottom - ALIGN_VALUE (sizeof (EFI_HOB_MEMORY_ALLOCATION), 8)) >> EFI_PAGE_SHIFT;\r
   //\r
-  // Verify that there is sufficient memory to satisfy the allocation\r
+  // The number of remaining pages needs to be greater than or equal to that of the request pages.\r
   //\r
-  if (*(FreeMemoryTop) - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < \r
-      *(FreeMemoryBottom)) {\r
-    DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%x Pages is available.\n", Pages));\r
-    DEBUG ((EFI_D_ERROR, "There is only left 0x%x pages memory resource to be allocated.\n", \\r
-    EFI_SIZE_TO_PAGES ((UINTN) (*(FreeMemoryTop) - *(FreeMemoryBottom)))));\r
+  Pages = ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (Granularity));\r
+  if (RemainingPages < Pages) {\r
+    DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64) Pages));\r
+    DEBUG ((EFI_D_ERROR, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64) RemainingPages));\r
     return  EFI_OUT_OF_RESOURCES;\r
   } else {\r
     //\r
@@ -208,12 +257,14 @@ PeiAllocatePages (
 \r
 /**\r
 \r
-  Memory allocation service on the CAR.\r
-\r
+  Pool allocation service. Before permanent memory is discoveried, the pool will\r
+  be allocated the heap in the temporary memory. Genenrally, the size of heap in temporary \r
+  memory does not exceed to 64K, so the biggest pool size could be allocated is \r
+  64K.\r
 \r
-  @param PeiServices     - The PEI core services table.\r
-  @param Size            - Amount of memory required\r
-  @param Buffer          - Address of pointer to the buffer\r
+  @param PeiServices               An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param Size                      Amount of memory required\r
+  @param Buffer                    Address of pointer to the buffer\r
 \r
   @retval EFI_SUCCESS              The allocation was successful\r
   @retval EFI_OUT_OF_RESOURCES     There is not enough heap to satisfy the requirement\r
@@ -223,26 +274,34 @@ PeiAllocatePages (
 EFI_STATUS\r
 EFIAPI\r
 PeiAllocatePool (\r
-  IN CONST EFI_PEI_SERVICES           **PeiServices,\r
-  IN UINTN                      Size,\r
-  OUT VOID                      **Buffer\r
+  IN CONST EFI_PEI_SERVICES     **PeiServices,\r
+  IN       UINTN                Size,\r
+  OUT      VOID                 **Buffer\r
   )\r
 {\r
   EFI_STATUS               Status;\r
   EFI_HOB_MEMORY_POOL      *Hob;\r
 \r
- //\r
- // If some "post-memory" PEIM wishes to allocate larger pool,\r
- // it should use AllocatePages service instead.\r
- //\r
- ASSERT (Size < 0x10000 - sizeof (EFI_HOB_MEMORY_POOL));\r
- Status = PeiServicesCreateHob (\r
+  //\r
+  // If some "post-memory" PEIM wishes to allocate larger pool,\r
+  // it should use AllocatePages service instead.\r
+  //\r
+  \r
+  //\r
+  // Generally, the size of heap in temporary memory does not exceed to 64K,\r
+  // HobLength is multiples of 8 bytes, so the maxmium size of pool is 0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL)\r
+  //\r
+  if (Size > (0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL))) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  \r
+  Status = PeiServicesCreateHob (\r
              EFI_HOB_TYPE_MEMORY_POOL,\r
              (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r
              (VOID **)&Hob\r
              );\r
+  ASSERT_EFI_ERROR (Status);\r
   *Buffer = Hob+1;  \r
 \r
-\r
   return Status;\r
 }\r