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
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
\r
}\r
\r
//\r
- // Check to see if on 4k boundary, If not aligned, make the allocation aligned.\r
+ // Check to see if on correct boundary for the memory type.\r
+ // If not aligned, make the allocation aligned.\r
//\r
- *(FreeMemoryTop) -= *(FreeMemoryTop) & 0xFFF;\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
// Verify that there is sufficient memory to satisfy the allocation.\r
//\r
// The number of remaining pages needs to be greater than or equal to that of the request pages.\r
//\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