2 EFI PEI Core memory services
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Initialize the memory services.
22 @param PrivateData Add parameter description
23 @param SecCoreData Points to a data structure containing information about the PEI core's operating
24 environment, such as the size and location of temporary RAM, the stack location and
26 @param OldCoreData Pointer to the PEI Core data.
27 NULL if being run in non-permament memory mode.
31 InitializeMemoryServices (
32 IN PEI_CORE_INSTANCE
*PrivateData
,
33 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
,
34 IN PEI_CORE_INSTANCE
*OldCoreData
38 PrivateData
->SwitchStackSignal
= FALSE
;
40 if (OldCoreData
== NULL
) {
42 PrivateData
->PeiMemoryInstalled
= FALSE
;
44 PrivateData
->BottomOfCarHeap
= SecCoreData
->PeiTemporaryRamBase
;
45 PrivateData
->TopOfCarHeap
= (VOID
*)((UINTN
)(PrivateData
->BottomOfCarHeap
) + SecCoreData
->PeiTemporaryRamSize
);
46 PrivateData
->SizeOfTemporaryMemory
= SecCoreData
->TemporaryRamSize
;
47 PrivateData
->StackSize
= (UINT64
) SecCoreData
->StackSize
;
50 PrivateData
->SizeOfCacheAsRam
= SecCoreData
->PeiTemporaryRamSize
+ SecCoreData
->StackSize
;
51 PrivateData
->MaxTopOfCarHeap
= (VOID
*) ((UINTN
) PrivateData
->BottomOfCarHeap
+ (UINTN
) PrivateData
->SizeOfCacheAsRam
);
52 PrivateData
->StackBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) SecCoreData
->StackBase
;
53 PrivateData
->StackSize
= (UINT64
) SecCoreData
->StackSize
;
56 PrivateData
->HobList
.Raw
= PrivateData
->BottomOfCarHeap
;
58 PeiCoreBuildHobHandoffInfoTable (
59 BOOT_WITH_FULL_CONFIGURATION
,
60 (EFI_PHYSICAL_ADDRESS
) (UINTN
) PrivateData
->BottomOfCarHeap
,
61 (UINTN
) SecCoreData
->PeiTemporaryRamSize
65 // Set PS to point to ServiceTableShadow in Cache
67 PrivateData
->PS
= &(PrivateData
->ServiceTableShadow
);
75 Install the permanent memory is now available.
76 Creates HOB (PHIT and Stack).
78 @param PeiServices - The PEI core services table.
79 @param MemoryBegin - Start of memory address.
80 @param MemoryLength - Length of memory.
82 @return EFI_SUCCESS Always success.
88 IN CONST EFI_PEI_SERVICES
**PeiServices
,
89 IN EFI_PHYSICAL_ADDRESS MemoryBegin
,
90 IN UINT64 MemoryLength
93 PEI_CORE_INSTANCE
*PrivateData
;
95 DEBUG ((EFI_D_INFO
, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin
, MemoryLength
));
96 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
98 PrivateData
->PhysicalMemoryBegin
= MemoryBegin
;
99 PrivateData
->PhysicalMemoryLength
= MemoryLength
;
100 PrivateData
->FreePhysicalMemoryTop
= MemoryBegin
+ MemoryLength
;
102 PrivateData
->SwitchStackSignal
= TRUE
;
109 Memory allocation service on permanent memory,
110 not usable prior to the memory installation.
113 @param PeiServices - The PEI core services table.
114 @param MemoryType - Type of memory to allocate.
115 @param Pages - Number of pages to allocate.
116 @param Memory - Pointer of memory allocated.
118 @retval EFI_SUCCESS The allocation was successful
119 @retval EFI_INVALID_PARAMETER Only AllocateAnyAddress is supported.
120 @retval EFI_NOT_AVAILABLE_YET Called with permanent memory not available
121 @retval EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement
122 to allocate the number of pages.
128 IN CONST EFI_PEI_SERVICES
**PeiServices
,
129 IN EFI_MEMORY_TYPE MemoryType
,
131 OUT EFI_PHYSICAL_ADDRESS
*Memory
134 PEI_CORE_INSTANCE
*PrivateData
;
135 EFI_PEI_HOB_POINTERS Hob
;
136 EFI_PHYSICAL_ADDRESS Offset
;
137 EFI_PHYSICAL_ADDRESS
*FreeMemoryTop
;
138 EFI_PHYSICAL_ADDRESS
*FreeMemoryBottom
;
140 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
141 Hob
.Raw
= PrivateData
->HobList
.Raw
;
144 // Check if Hob already available
146 if (!PrivateData
->PeiMemoryInstalled
) {
148 // When PeiInstallMemory is called but CAR has *not* been moved to temporary memory,
149 // the AllocatePage will dependent the field of PEI_CORE_INSTANCE structure.
151 if (!PrivateData
->SwitchStackSignal
) {
152 return EFI_NOT_AVAILABLE_YET
;
154 FreeMemoryTop
= &(PrivateData
->FreePhysicalMemoryTop
);
155 FreeMemoryBottom
= &(PrivateData
->PhysicalMemoryBegin
);
158 FreeMemoryTop
= &(Hob
.HandoffInformationTable
->EfiFreeMemoryTop
);
159 FreeMemoryBottom
= &(Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
);
165 // Check to see if on 4k boundary
167 Offset
= *(FreeMemoryTop
) & 0xFFF;
170 // If not aligned, make the allocation aligned.
173 *(FreeMemoryTop
) -= Offset
;
177 // Verify that there is sufficient memory to satisfy the allocation
179 if (*(FreeMemoryTop
) - ((Pages
* EFI_PAGE_SIZE
) + sizeof (EFI_HOB_MEMORY_ALLOCATION
)) <
180 *(FreeMemoryBottom
)) {
181 DEBUG ((EFI_D_ERROR
, "AllocatePages failed: No 0x%x Pages is available.\n", Pages
));
182 DEBUG ((EFI_D_ERROR
, "There is only left 0x%x pages memory resource to be allocated.\n", \
183 EFI_SIZE_TO_PAGES ((UINTN
) (*(FreeMemoryTop
) - *(FreeMemoryBottom
)))));
184 return EFI_OUT_OF_RESOURCES
;
187 // Update the PHIT to reflect the memory usage
189 *(FreeMemoryTop
) -= Pages
* EFI_PAGE_SIZE
;
192 // Update the value for the caller
194 *Memory
= *(FreeMemoryTop
);
197 // Create a memory allocation HOB.
199 BuildMemoryAllocationHob (
201 Pages
* EFI_PAGE_SIZE
,
211 Memory allocation service on the CAR.
214 @param PeiServices - The PEI core services table.
215 @param Size - Amount of memory required
216 @param Buffer - Address of pointer to the buffer
218 @retval EFI_SUCCESS The allocation was successful
219 @retval EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement
220 to allocate the requested size.
226 IN CONST EFI_PEI_SERVICES
**PeiServices
,
232 EFI_HOB_MEMORY_POOL
*Hob
;
235 // If some "post-memory" PEIM wishes to allocate larger pool,
236 // it should use AllocatePages service instead.
238 ASSERT (Size
< 0x10000 - sizeof (EFI_HOB_MEMORY_POOL
));
239 Status
= PeiServicesCreateHob (
240 EFI_HOB_TYPE_MEMORY_POOL
,
241 (UINT16
)(sizeof (EFI_HOB_MEMORY_POOL
) + Size
),